【问题标题】:Issue when using Entity Framework and PostgreSQL使用实体框架和 PostgreSQL 时的问题
【发布时间】:2018-06-13 19:35:17
【问题描述】:

我目前在使用 Entity Framework 6 和 PostgreSQL 时遇到问题,当我创建一个没有任何关系的表时,它工作得很好,但是当我使用一个关系时,我收到如下错误:

ReferentialConstraint 中的依赖属性映射到 商店生成的列。列:“person_type_id”。

我仔细检查了我的表格,一切似乎都配置正确,例如检查这两个 PersonPerson Type

如您所见,在 Person 中,列 person_type_id 充当 Person Type 表的外键,而 person_id 是主键。

这是我正在尝试实现的代码:

//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();

 我还尝试通过返回 Person Type 的实例来分配关系本身,但我得到了完全相同的错误,我的意思是:

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();

正如您将在附加的屏幕截图中看到的,变量 typeInstance 实际上是返回 ID 为 1 的 Person 类型的实例并分配它,我认为也许通过“强制”关联它会起作用,但它只是没有't,我一遍又一遍地得到同样的错误。

有什么建议吗?

模型类(由实体框架生成)

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; }
}

FK 图片:

【问题讨论】:

  • 你能分享PersonPersonType模型类吗?
  • 您可能在表之间定义了错误的列关系。请分享您的实体类代码,为您提供准确的解决方案。
  • 已经将它们添加到原帖中,谢谢大家
  • 我认为person_type_id 设置为StoreGeneratedPattern="Identity"。您需要将其设置为“无”。基本上你的模型有问题。

标签: c# asp.net database entity-framework


【解决方案1】:

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

【讨论】:

  • Hafiz,我刚刚添加了 FK 的屏幕截图,实际上已经设置好了。
  • 你有外键自动增量吗?你有主键的自动增量吗?
【解决方案2】:

感谢CodingYoshi 我能够解决这个问题,显然 PostgreSQL 将所有 PK 和 FK 生成为 Serial,默认情况下它后面有一个自动增量声明。这并不意味着模型是错误的,但似乎 Entity Framework 将其解释为自动增量。

将 StoreGeneratedPattern="Identity" 设置为“None”就可以了。

【讨论】:

    猜你喜欢
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多