【问题标题】:MVC Model Poco class, design issue,MVC 模型 Poco 类,设计问题,
【发布时间】:2013-03-22 22:26:15
【问题描述】:

我有一个小问题。

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

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

谁能告诉我我应该如何设计它?

我首先使用 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; }
    }

【问题讨论】:

    标签: asp.net-mvc entity-framework asp.net-mvc-4 poco


    【解决方案1】:

    在您的类的以下属性和注释中,它应该有助于 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 之间添加级联删除,那么您将收到现在的错误。

    【讨论】:

    • 当我使用它时,我在尝试显示公司时收到此错误消息:在表“客户”上引入外键约束“FK_dbo.Customer_dbo.Company_CompanyId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。
    • 这是另一个问题,我已经更新了我的帖子,提供了更多的解释和图片。
    • 我不知道是不是只有我,但是我找不到你显示的页面,是在VS2012还是在sql manager?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2010-11-23
    • 1970-01-01
    相关资源
    最近更新 更多