我尝试使用LinQ创建数据库结构。这些表互相引用。根据SO的提示,我目前有以下代码:(部分)
[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; }
}
}
当调用CreateDatabase()时,我收到以下错误:
部署步骤“激活功能”:列中发生错误 'tqStandort.Id'与引用列的数据类型不同 外键'FK_Benutzer_Standort'中的'tqBenutzer.IdStandort'。可以 不创建约束
我的错误在哪里?
(UPDATE): 这将是我想要实现的SQL结构:
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])
答案 0 :(得分:1)
您在关联中使用的列应具有相同的类型。 改为:
private long? _idStandort;
或:
[Table(Name = "tqStandort")]
public class Standort
{
[Column(IsPrimaryKey = true, DbType = "Int IDENTITY NOT NULL", IsDbGenerated = true, UpdateCheck = UpdateCheck.Never)]
public int Id;
...
}