0

I try to create a database structure using LinQ. The tables have references to each other. Following the hints here on SO I currently have the following Code: (part of)

[Database]
public class SqlData : DataContext
{     
    public Table<Benutzer> Benutzers;      
    public Table<Standort> Standorts;

    public SqlData(string connectionString) : base(connectionString)   { }

    public void CreateDb()
    {            
        CreateDatabase();
    }
}

[Table(Name = "tqStandort")]
public class Standort
{
    [Column(IsPrimaryKey = true, DbType = "BigInt IDENTITY NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
    public int Id;
    [Column]
    public string Name;
}

[Table(Name = "tqBenutzer")]
public class Benutzer
{
    [Column(IsPrimaryKey = true, DbType = "BigInt IDENTITY NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
    public int Id;

    [Column(Name = "IdStandort")]
    private int? _idStandort;
    private EntityRef<Standort> _standort;

   [Association(Name = "FK_Benutzer_Standort", IsForeignKey = true, Storage = "_standort", ThisKey = "_idStandort")]
    public Standort IdStandort
    {
        get { return _standort.Entity; }
        set { _standort.Entity = value; }
    }
}

When CreateDatabase() is called I receive the following Error:

Error occurred in deployment step 'Activate Features': Column 'tqStandort.Id' is not the same data type as referencing column 'tqBenutzer.IdStandort' in foreign key 'FK_Benutzer_Standort'. Could not create constraint

Where is my mistake?

(UPDATE): This would be the SQL-Structure I would like to achieve:

CREATE TABLE [dbo].[tqStandort](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NULL,
 CONSTRAINT [PK_tqStandort] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[tqBenutzer](
    [Id] [int] NOT NULL,
    [IdStandort] [int] NULL,
 CONSTRAINT [PK_tqBenutzer] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tqBenutzer]  WITH CHECK ADD  CONSTRAINT [FK_tqBenutzer_tqStandort] FOREIGN KEY([IdStandort])
REFERENCES [dbo].[tqStandort] ([Id])

1 Answer 1

1

Columns that you use in association should have same type. Change to:

private long? _idStandort;

or:

[Table(Name = "tqStandort")]
public class Standort
{
    [Column(IsPrimaryKey = true, DbType = "Int IDENTITY NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
    public int Id;

    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just one second before I found out myself. :) Yep. Stupid mistake Thanks, Pal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.