EF首先使用一对多关系创建重复的外键 [英] EF using code first with one-to-many relationship creating duplicate foreign keys

查看:434
本文介绍了EF首先使用一对多关系创建重复的外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下课程进行一对多关系:

  public class Supplier 
{
public Supplier()
{
SupplierId = Guid.NewGuid();
}

public Guid SupplierId {get;组;
[必需]
public string Name {get;组; }

[必需]
public virtual Address Address {get;组; }

public virtual ICollection< Contact>联系人{get;组;


public class联系
{
public Contact()
{
ContactId = Guid.NewGuid();
}

public Guid ContactId {get;组;

[必需]
public string FirstName {get;组; }
[必需]
public string LastName {get;组; }

[必需]
public Guid SupplierId {get;组; }
[ForeignKey(SupplierId)]
public virtual供应商供应商{get;组;
}

我正在使用ASP.NET MVC 4,并创建一个新的联系人,SupplierId在querystring中传递并添加到Contact create form作为隐藏字段:

  @ Html.HiddenFor model => model.SupplierId)

这将创建联系人成功并填充联系人的SupplierId字段, Guid从查询字符串。但是,问题是Contacts表具有名为Supplier_SupplierId的虚拟供应商关系的另一个字段,此字段为NULL。我不知道我明白为什么甚至在Contacts表中创建Supplier_SupplierId,因为SupplierId应该被用作外键,这只是多余的。如果我尝试访问供应商的联系人集合,我成功添加了一个联系人,该集合返回为空,因为Contact.Supplier_SupplierId为NULL。但是,如果我将SupplierId列中的值复制到联系人记录的Supplier_SupplierId列中,Supplier.Contacts将带回联系人。



这是我的联系人表最终看起来像:

  CREATE TABLE [dbo]。[Contacts](
[ContactId] UNIQUEIDENTIFIER NOT NULL,
[FirstName] NVARCHAR(MAX)NOT NULL,
[LastName] NVARCHAR(MAX)NOT NULL,
[Supplier_SupplierId] UNIQUEIDENTIFIER NULL,
CONSTRAINT [PK_dbo.Contacts] PRIMARY KEY CLUSTERED([ContactId] ASC),
CONSTRAINT [FK_dbo.Contacts_dbo.Suppliers_SupplierId] FOREIGN KEY([SupplierId])REFERENCES [dbo]。[供应商]([SupplierId]),
CONSTRAINT [FK_dbo.Contacts_dbo .Suppliers_Supplier_SupplierId] FOREIGN KEY([Supplier_SupplierId])REFERENCES [dbo]。[供应商]([SupplierId])
);有没有人知道为什么在联系人中创建供应商的2个外键?如何更改它,以便供应商的唯一的外键是SupplierId?

解决方案

此映射是错误的(我指的是在你的问题下面你的评论):

  modelBuilder.Entity< Contact>()
.HasRequired(r = ; r.Supplier)
.WithMany()
.HasForeignKey(f => f.SupplierId)
.WillCascadeOnDelete(false);

您需要:

 
$ b $ W $($)
$ H (f => f.SupplierId)
.WillCascadeOnDelete(false);

否则,EF将考虑 Supplier.Contacts as属于供应商联系人之间的另一个和第二个一对多关系的导航属性,并介绍第二个外键。


I'm using the following classes for a one-to-many relationship:

public class Supplier
{
    public Supplier()
    {
        SupplierId = Guid.NewGuid();
    }

    public Guid SupplierId { get; set; }
    [Required]
    public string Name { get; set; }

    [Required]
    public virtual Address Address { get; set; }

    public virtual ICollection<Contact> Contacts { get; set; }
}

public class Contact
{
    public Contact()
    {
        ContactId = Guid.NewGuid();
    }

    public Guid ContactId { get; set; }

    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }

    [Required]
    public Guid SupplierId { get; set; }
    [ForeignKey("SupplierId")]
    public virtual Supplier Supplier { get; set; }
}

I'm using ASP.NET MVC 4 and upon Create of a new Contact, the SupplierId is passed in the querystring and added to the Contact create form as a hidden field:

@Html.HiddenFor(model => model.SupplierId)

This creates the Contact successfully and populate's the Contact's SupplierId field with the Guid from the query string. However, the problem is that the Contacts table has another field for the virtual Supplier relationship called Supplier_SupplierId and this field is NULL. I'm not sure I understand why Supplier_SupplierId is even being created on the Contacts table, as SupplierId should be being used as the foreign key and this would just be redundant. If I try to access the Contacts collection of a Supplier I successfully added a Contact to, the collection comes back empty because Contact.Supplier_SupplierId is NULL. However, if I copy the value from the SupplierId column into the Supplier_SupplierId column of the Contact record, Supplier.Contacts will bring back the Contact.

This is what my Contacts table ends up looking like:

CREATE TABLE [dbo].[Contacts] (
    [ContactId]           UNIQUEIDENTIFIER NOT NULL,
    [FirstName]           NVARCHAR (MAX)   NOT NULL,
    [LastName]            NVARCHAR (MAX)   NOT NULL,
    [Supplier_SupplierId] UNIQUEIDENTIFIER NULL,
    CONSTRAINT [PK_dbo.Contacts] PRIMARY KEY CLUSTERED ([ContactId] ASC),
    CONSTRAINT [FK_dbo.Contacts_dbo.Suppliers_SupplierId] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Suppliers] ([SupplierId]),
    CONSTRAINT [FK_dbo.Contacts_dbo.Suppliers_Supplier_SupplierId] FOREIGN KEY ([Supplier_SupplierId]) REFERENCES [dbo].[Suppliers] ([SupplierId])
);

Does anyone know why 2 foreign keys to Suppliers are being created in Contacts? How can I change it so that the only foreign key to Suppliers is SupplierId?

解决方案

This mapping is wrong (I'm refering to your comment below your question):

modelBuilder.Entity<Contact>()
    .HasRequired(r => r.Supplier)
    .WithMany()
    .HasForeignKey(f => f.SupplierId)
    .WillCascadeOnDelete(false);

You need:

modelBuilder.Entity<Contact>()
    .HasRequired(r => r.Supplier)
    .WithMany(s => s.Contacts)
    .HasForeignKey(f => f.SupplierId)
    .WillCascadeOnDelete(false);

Otherwise EF will consider Supplier.Contacts as a navigation property that belongs to another and second one-to-many relationship between Supplier and Contact - and that introduces the second foreign key.

这篇关于EF首先使用一对多关系创建重复的外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆