1

我有一个小问题。

如果我保存了所有应用程序的地址,我正在尝试创建一个地址类。

问题是我希望能够将多个地址链接到客户和公司。

有人可以告诉我应该如何设计吗?

我首先使用 MVC 4 和 entityFramework 代码。

 public class Address
    {
        [Key]
        public int AddressId { get; set; }

        public string Street { get; set; }

        public string Number { get; set; }

        public string ZipCode { get; set; }

        public int CountyId { get; set; }
        public virtual County County { get; set; }

        public int StateId { get; set; }
        public virtual State State { get; set; }

        public int CountryId { get; set; }
        public virtual Country Country { get; set; }
    }

public class Customer
    {
        [Key]
        public int CustomerId { get; set; }
        public int CompanyId { get; set; }

        [Display(Name = "Kund")]
        public string Name { get; set; }

        public virtual Company Company { get; set; }

        // wan't to display a ICollection of addresses.
        //public virtual ICollection<Address> Addresses { get; set; }
    }

 public class Company
    {
        [Key]
        public int CompanyId { get; set; }
        [Display(Name = "Organisationsnummer")]
        public string OrganisationNumber { get; set; }
        [Display(Name = "Företag")]
        public string Name { get; set; }
        [Display(Name = "Företag skapat")]
        public DateTime CreationDate { get; set; }

        public virtual ICollection<Customer> Customers { get; set; }
        public virtual ICollection<Employee> Employees { get; set; }
        // wan't to display a ICollection of addresses.
        //public virtual ICollection<Address> Addresses { get; set; }
    }
4

1 回答 1

2

在您的类的以下属性和注释中,它应该有助于 Entity Framework 理解您的关系:

 public class Address
{
    [Key]
    public int AddressId { get; set; }

    public string Street { get; set; }

    public string Number { get; set; }

    public string ZipCode { get; set; }

    public int CustomerId {get;set;}
    [ForeignKey("CustomerId")]
    public virtual Customer Customer {get;set;}

    public int CompanyId {get;set;}
    [ForeignKey("CompanyId")]
    public virtual Company Company {get;set;}

    public int CountyId { get; set; }
    [ForeignKey("CountyId")]
    public virtual County County { get; set; }

    public int StateId { get; set; }
    [ForeignKey("StateId")]
    public virtual State State { get; set; }

    public int CountryId { get; set; }
    [ForeignKey("CountryId")]
    public virtual Country Country { get; set; }
}

public class Customer
{
    [Key]
    public int CustomerId { get; set; }

    public int CompanyId { get; set; }

    [Display(Name = "Kund")]
    public string Name { get; set; }

    [ForeignKey("CompanyId")]
    public virtual Company Company { get; set; }

    [InverseProperty("Customer")]
    public virtual ICollection<Address> Addresses { get; set; }
}



public class Company
{
    [Key]
    public int CompanyId { get; set; }
    [Display(Name = "Organisationsnummer")]
    public string OrganisationNumber { get; set; }
    [Display(Name = "Företag")]
    public string Name { get; set; }
    [Display(Name = "Företag skapat")]
    public DateTime CreationDate { get; set; }

    [InverseProperty("Company")]
    public virtual ICollection<Customer> Customers { get; set; }

    [InverseProperty("Company")]
    public virtual ICollection<Address> Addresses { get; set; }

    [InverseProperty("Company")]
    public virtual ICollection<Employee> Employees { get; set; }

}

public class Employee
{
[Key]
public int EmployeeId {get;set;}

public int CompanyId {get;set;}

[ForeignKey("CompanyId")]
public virtual Company Company {get;set;}
}

编辑:你现在有另一种类型的问题。您的 DELETE/UPDATE 规则导致您现在看到的错误。您很可能在通向同一主键表的多个路径上设置了 CASCADE delete。首先设置您的所有关系,如下所示:

在此处输入图像描述 在此处输入图像描述

然后假设这种情况:您有实体 A、B 和 C。实体 B 对实体 A 具有 FK。实体 C 对实体 A 和实体 B 具有 FK。

您只能在一个依赖路径上设置级联删除/更新。这意味着您只能执行以下操作:

A和B之间的级联删除和B和C之间的级联删除。

但是,如果您在 A 和 C 之间添加级联删除以及上面的内容,您将收到现在的错误。

于 2013-03-23T05:34:29.280 回答