设置一对一的关系

时间:2014-11-25 22:45:35

标签: c# sql-server entity-framework

如何使用流畅的API在Entity Framework的代码中正确设置一对一关系?

根模型是Project类。每个项目都包含一个优惠。它也可以选择包含一个客户订单。

历史模型的目的是包含相应非历史行的快照(添加Project时,创建新Project并复制到新ProjectHistorical } class,如果已编辑,则会创建一个新的ProjectHistorical类,其中包含已编辑的Project的数据。 历史对象应AutoMapper友好。您可以将历史模型视为一种愚蠢,琐碎和天真的解决方法,因为数据库引擎中缺少临时数据库支持。

使用此配置,我遇到以下困难:

  1. CustomerOrdersOffers表及其历史记录 对应方生成了Project_Id列。我找不到办法了 如何强制使用Id主键或注释 ProjectId属性(我更喜欢使用注释的ProjectId属性)。
  2. ProjectHistoricals表有 生成了CustomerOrder_IdOffer_Id列。我找不到办法了 如何强制使用Id列(请注意,Id 列不是历史表的主键)或注释 CustomerOrderIdOfferId列(ProjectProjectHistorical CustomerOrderId个班级;我更喜欢使用注释的OfferIdpublic class Project { public virtual int Id { get; set; } // public virtual int OfferId { get; set; } public virtual Offer Offer { get; set; } // public virtual int? CustomerOrderId { get; set; } public virtual CustomerOrder CustomerOrder { get; set; } } public class ProjectHistorical { public virtual int LogId { get; set; } public virtual int Id { get; set; } // public virtual int OfferId { get; set; } public virtual Offer Offer { get; set; } // public virtual int? CustomerOrderId { get; set; } public virtual CustomerOrder CustomerOrder { get; set; } } public class CustomerOrder { public virtual int Id { get; set; } // public virtual int ProjectId { get; set; } public virtual Project Project { get; set; } } public class CustomerOrderHistorical { public virtual int LogId { get; set; } public virtual int Id { get; set; } // public virtual int ProjectId { get; set; } public virtual Project Project { get; set; } } public class Offer { public virtual int Id { get; set; } // public virtual int ProjectId { get; set; } public virtual Project Project { get; set; } } public class OfferHistorical { public virtual int LogId { get; set; } public virtual int Id { get; set; } // public virtual int ProjectId { get; set; } public virtual Project Project { get; set; } } 列。
  3. 我试图搜索了很多东西并尝试了更多的东西,但没有任何工作(我本来应该)。

    我有以下型号:

    Id

    以下流畅的api(主键必须始终命名为modelBuilder.Conventions.Remove<IdKeyDiscoveryConvention>(); modelBuilder.Conventions.Remove<ForeignKeyAssociationMultiplicityConvention>(); modelBuilder.Conventions.Remove<PrimaryKeyNameForeignKeyDiscoveryConvention>(); modelBuilder.Conventions.Remove<OneToOneConstraintIntroductionConvention>(); modelBuilder.Conventions.Remove<TypeNameForeignKeyDiscoveryConvention>(); modelBuilder.Conventions.Remove<AssociationInverseDiscoveryConvention>(); modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>(); modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>(); modelBuilder.Entity<CustomerOrder>().HasKey(t => t.Id); modelBuilder.Entity<CustomerOrderHistorical>().HasKey(t => t.LogId); modelBuilder.Entity<CustomerOrderHistorical>().HasRequired(t => t.Project); modelBuilder.Entity<Offer>().HasKey(t => t.Id); modelBuilder.Entity<OfferHistorical>().HasKey(t => t.LogId); modelBuilder.Entity<OfferHistorical>().HasRequired(t => t.Project); modelBuilder.Entity<Project>().HasKey(t => t.Id); modelBuilder.Entity<Project>().HasRequired(t => t.Offer).WithRequiredPrincipal(t => t.Project); modelBuilder.Entity<Project>().HasOptional(t => t.CustomerOrder).WithRequired(t => t.Project); modelBuilder.Entity<ProjectHistorical>().HasKey(t => t.LogId); modelBuilder.Entity<ProjectHistorical>().HasRequired(t => t.Offer); modelBuilder.Entity<ProjectHistorical>().HasOptional(t => t.CustomerOrder); ,因为所有模型都实现了共享接口,而不是应用程序的通用部分所使用的接口):

            CreateTable(
                "dbo.CustomerOrderHistoricals",
                c => new
                    {
                        LogId = c.Int(nullable: false, identity: true),
                        Id = c.Int(nullable: false),
                        Project_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.LogId)
                .ForeignKey("dbo.Projects", t => t.Project_Id)
                .Index(t => t.Project_Id);
    
            CreateTable(
                "dbo.Projects",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                    })
                .PrimaryKey(t => t.Id);
    
            CreateTable(
                "dbo.CustomerOrders",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Project_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Projects", t => t.Project_Id)
                .Index(t => t.Project_Id);
    
            CreateTable(
                "dbo.Offers",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Project_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Projects", t => t.Project_Id)
                .Index(t => t.Project_Id);
    
            CreateTable(
                "dbo.OfferHistoricals",
                c => new
                    {
                        LogId = c.Int(nullable: false, identity: true),
                        Id = c.Int(nullable: false),
                        Project_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.LogId)
                .ForeignKey("dbo.Projects", t => t.Project_Id)
                .Index(t => t.Project_Id);
    
            CreateTable(
                "dbo.ProjectHistoricals",
                c => new
                    {
                        LogId = c.Int(nullable: false, identity: true),
                        Id = c.Int(nullable: false),
                        CustomerOrder_Id = c.Int(),
                        Offer_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.LogId)
                .ForeignKey("dbo.CustomerOrders", t => t.CustomerOrder_Id)
                .ForeignKey("dbo.Offers", t => t.Offer_Id)
                .Index(t => t.CustomerOrder_Id)
                .Index(t => t.Offer_Id);
    

    这是实体框架将模型编译到数据库的方式:

    {{1}}

1 个答案:

答案 0 :(得分:0)

您可以阅读this tutorial或在stackoverflow上查看this answer或在MSDN上查看this answer