繁体   English   中英

实体框架Fluent API无法一对一插入

[英]Entity Framework Fluent API Cannot Insert one-to-one

我正在尝试使用Entity Framework将对象插入数据库表中,并首先使用代码(流利的api)。 在执行此操作的同时,我不断遇到以下错误之一:

1)InvalidOperationException:ReferentialConstraint中的从属属性映射到商店生成的列。 列:“ Id”

2)无法将IDENTITY_INSERT设置为OFF的值插入标识列

我的关系是一对一的,但是也许我可以重做或构造数据库来完成我想要的事情。 我也曾考虑过要使用一个从零到零或区域,即使总是需要另一个对象。

因此,我将以下数据库表映射到这些C#对象中(使用虚拟映射):

public class test
{
    [Key]
    public long Id { get; set; }
    public DateTime ResultDate { get; set; }

    public virtual test_additional test_additional { get; set; }
    public virtual test_status test_status { get; set; }
}

public class test_additional
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long TestId { get; set; } //Foreign Key to test
    ...

    public virtual test test { get; set; }
}

public class test_status {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long TestId { get; set; } //Foreign Key to Test

    public long TestFormId { get; set; } //this is the object I want to insert, Foreign key to the Primary key of test_form
    ...

    public virtual test test { get; set; }
    public virtual test_form test_form { get; set; } //object mapping
}

public class test_form {
    [Key]
    public long Id { get; set; } //Primary Key

    public string FileName { get; set; }

    public virtual test_status test_status { get; set; }
}

因此,我删除了一些非常简单的对象,以简化易读性,剥离了该功能所需的成员/列。

因此,有些test对象具有可选的test_additionaltest_status

它们以一对零或一对一的关系生成。 哪个工作正常,我的关系定义为:

modelBuilder.Entity<test>()
.HasOptional(e => e.test_additional)
.WithRequired(e =>e.test);

modelBuilder.Entity<test>()
.HasOptional(e => e.test_status)
.WithRequired(e => e.test);

现在,我有麻烦的实体是test_form ,如果test_status定义应该永远是一个test_form与此有关。 我目前的关系定义为:

modelBuilder.Entity<test_form>()
.HasRequired(e => e.test_status)
.WithRequiredDependent(e => e.test_form);

另外,我尝试附加此配置:

modelBuilder.Entity<test_status>()
.HasKey(e => e.TestFormId);

-

这是在数据库中插入此对象的简单实现:

try {
    test UserTest = new test { ResultDate = DateTime.Now; }

    UOW.test.Insert(UserTest);
    UOW.Save();

    test_additional ta = new test_additional { TestId = UserTest.Id; }
    test_form tf = new test_form { FileName = "Testing.pdf"; }

    UOW.test_additional.Insert( ta );
    UOW.test_form.Insert( tf );
    UOW.Save(); //This is where it will throw that error.

    test_status status = new test_status {
        TestId = UserTest.Id;
        TestFormId = tf.Id;
    }

    UOW.test_status.Insert( status );
    UOW.Save();
} catch {
    throw;
}

-

在保存工作单位之前,我已经使用BreakPoints,并且可以确认test_form对象中的Id是test_form的默认值(即0)。因此,我没有 明确设置Identity Column 在删除test_form (在实现的方法中)后,我可以插入到test_additional类别中并保存, 没有问题

所以我的问题是……我的实体关系定义正确吗? test_form对象使用额外的“一对零或一个”会更聪明吗? 为什么不能将这个简单的对象插入数据库?

我还考虑过将test_form中的虚拟test_form对象定义为ICollection ,然后可以使用.HasMany(e => e.test_form).HasForeignKey(e => e.TestFormId); 因此即使我只将1个项目用于test_status ,它也将绑定到外键。

意见? 我靠近吗?

再次感谢您抽出宝贵时间阅读我的问题!

我有你的问题。 只要删除您的数据库和迁移文件即可。 之后,添加新迁移以创建新数据库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM