如果某个地方有明确的答案,请道歉.但由于它包含一个外键,我无法插入到一个简单的表中.
任务表
> TaskId(PK)
>描述
> StatusId(FK)
我尝试插入其中:
Task t = new Task();
t.Id = 1234;
t.Title = "foo";
t.Status = db.Status.ToList().First();
但是获取updateException错误:
正在从AssociationSet’FK_Task_Status’添加或删除关系.对于基数约束,还必须添加或删除相应的“任务”.
我该如何插入此表?
干杯
….
发现我的问题……
我的架构错了.当我创建我的外键时,我将它指向错误的字段.看看SQL分析器,看到了这个:
SELECT
1 AS [C1],
[Extent1].[Id] AS [Id],
[Extent1].[Descr] AS [Descr],
[Extent2].[Id] AS [Id1]
FROM [dbo].[Status] AS [Extent1]
LEFT OUTER JOIN [dbo].[Task] AS [Extent2] ON [Extent1].[Id] = [Extent2].[Id]
应该是这个(加入statusId而不是id):
SELECT
1 AS [C1],
[Extent1].[Id] AS [Id],
[Extent1].[Descr] AS [Descr],
[Extent2].[Id] AS [Id1]
FROM [dbo].[Status] AS [Extent1]
LEFT OUTER JOIN [dbo].[Task] AS [Extent2] ON [Extent1].[Id] = [Extent2].[StatusId]
傻我;)
最佳答案 在.net framework 4.0中你可以使用这种简单的方法:
Task t = new Task();
t.Id = 1234;
t.Title = "foo";
t.StatusId = 5678;