简体   繁体   English

使用Entity Framework和PostgreSQL时的问题

[英]Issue when using Entity Framework and PostgreSQL

I'm currently experiencing issues when using Entity Framework 6 and PostgreSQL, when I create a table that doesn't have any relationships it works just fine, but when I do one with one relationship I get an error like the following: 我目前在使用Entity Framework 6和PostgreSQL时遇到问题,当我创建一个没有任何关系的表时,它的工作正常,但是当我处理一个关系时,出现如下错误:

A dependent property in a ReferentialConstraint is mapped to a store-generated column. ReferentialConstraint中的从属属性映射到商店生成的列。 Column: 'person_type_id'. 列:“ person_type_id”。

I double checked my tables and everything seems to be configured correctly, check for instance this two Person and Person Type : 我仔细检查了我的表,似乎一切都配置正确,例如检查这两个PersonPerson Type

人和人类型表

As you can see there, in Person, the column person_type_id acts as foreign key to the Person Type table, and person_id is the primary key. 如您所见,在“人员”中,列person_type_id充当“人员类型”表的外键,而person_id是主键。

This is the code I'm trying to implement: 这是我要实现的代码:

//Creating db Context
postgresEntities db = new postgresEntities();

//Creation of Person new instance
person nPer = new person();

//Asigning person_type with ID = 1
//This makes reference to an existing record so the relationship can be set
nPer.person_type_id = 1;

//It says db.people because there's an option for collective nouns, it just changes the name to a 'more comprehensive one'

//Add Person to Database
db.people.Add(nPer);

//Commit
db.SaveChanges();

 I also tried assigning the Relationship itself by returning an instance of Person Type, but I get the exact same error, here what I mean: also我还尝试通过返回一个Person Type实例来分配Relationship本身,但是我得到了完全相同的错误,这是我的意思:

postgresEntities db = new postgresEntities();
person nPer = new person();

person_type typeInstance = db.person_type.FirstOrDefault(n => n.person_type_id == 1);

nPer.person_type = typeInstance;

db.people.Add(nPer);
db.SaveChanges();

As you will see in the attached screenshot the variable typeInstance is in fact returning the instance of the Person Type with the ID 1 and assigning it, I thought that maybe by 'forcing' the association it will work, but it just doesn't, I get the same exact error over and over. 如您在所附的屏幕快照中所见,变量typeInstance实际上是返回ID为1的Person Type的实例并为其分配,我认为可能是通过“强制”关联将起作用,但实际上不起作用,我一遍又一遍地得到相同的错误。

Any suggestions? 有什么建议么?

在此处输入图片说明

Model Classes (generated by Entity Framework) 模型类(由实体框架生成)

public partial class person_type
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public person_type()
    {
        this.people = new HashSet<person>();
    }

    public int person_type_id { get; set; }
    public string person_type1 { get; set; }
    public string person_description { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<person> people { get; set; }
}

} }

public partial class person
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public person()
    {
        this.addresses = new HashSet<address>();
        this.companies = new HashSet<company>();
        this.customers = new HashSet<customer>();
        this.emails = new HashSet<email>();
        this.employees = new HashSet<employee>();
        this.internal_customer = new HashSet<internal_customer>();
        this.phones = new HashSet<phone>();
    }

    public int person_id { get; set; }
    public int person_type_id { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<address> addresses { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<company> companies { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<customer> customers { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<email> emails { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<employee> employees { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<internal_customer> internal_customer { get; set; }
    public virtual person_type person_type { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<phone> phones { get; set; }
}

Image of FK: FK图片:

在此处输入图片说明

您没有将person_type_id定义为外键Person表。

Thanks to CodingYoshi I was able to work around this issue, apparently PostgreSQL generates all PKs and FKs as Serial, which by default has an auto increment declaration behind it. 多亏了CodingYoshi,我才能够解决此问题,显然PostgreSQL将所有PK和FK生成为Serial,默认情况下,其后面具有自动增量声明。 It doesn't mean that the model is wrong, but it seems like Entity Framework interprets it as an Auto Increment. 这并不意味着该模型是错误的,但似乎Entity Framework会将其解释为自动增量。

Setting the StoreGeneratedPattern="Identity" to "None" did the trick. 将StoreGeneratedPattern =“ Identity”设置为“ None”可以解决问题。

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

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