EF6 MVC5设置1-1关系

时间:2013-12-06 02:50:50

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

我首先使用Code启动并运行我的应用程序,我正在尝试设置1-1关系,但是当我更新数据库时,我收到错误“SupplyPointId:Name:类型中的每个属性名称必须是唯一的。属性名称“SupplyPointId”已经定义。“

我已尝试删除SupplyPointAddress.SupplyPointId上的现有索引约束,但这没有帮助。在另一张表中它是PK。任何评论都非常感谢

    public partial class SupplyPoint
    {
        [Key]
        //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

        public int SupplyPointId { get; set; }
        public string SPID { get; set; }
        public string SupplyPointName { get; set; }
        public int SupplyPointTypeId { get; set; }
        public DateTime SupplyPointEffectiveDateTime { get; set; }
        public string GazateerRef { get; set; }

        public virtual SupplyPointType SupplyPointType { get; set; }

        //[ForeignKey("SupplyPointId")]
        public virtual SupplyPointAddress SupplyPointAddress { get; set; }


    }
    public partial class SupplyPointAddress
    {
        [Key]
        //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int SupplyPointAddressId { get; set; }

        public int SupplyPointId { get; set; }

        public string D5001_FreeDescriptor { get; set; }


        public string D5002_SubBuildingName { get; set; }

        public string D5003_BuildingName { get; set; }

        public string D5004_BuildingNumber { get; set; }
        public string D5005_DependentThoroughfareName { get; set; }


        public string D5006_DependentThoroughfareDescriptor { get; set; }
        public string D5007_ThoroughfareName { get; set; }

        public string D5008_ThoroughfareDescriptor { get; set; }
        public string D5009_DoubleDependentLocality { get; set; }

        public string D5010_DependentLocality { get; set; }

        public string D5011_PostTown { get; set; }
        public string D5012_County { get; set; }
        public string D5013_Postcode { get; set; }

        public virtual SupplyPoint SupplyPoint { get; set; }

    }


public System.Data.Entity.DbSet<AscendancyCF.Models.SupplyPoint> SupplyPoints { get; set; }

public System.Data.Entity.DbSet<AscendancyCF.Models.SupplyPointAddress> SupplyPointAddresses { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

        modelBuilder.Entity<SupplyPointAddress>()
               .HasOptional<SupplyPoint>(u => u.SupplyPoint)
               .WithRequired(c => c.SupplyPointAddress).Map(p => p.MapKey("SupplyPointId"));


    base.OnModelCreating(modelBuilder);
}

2 个答案:

答案 0 :(得分:3)

我将外键移动到SupplyPoint表中,以便在SupplyPoint中将外键定义为SupplyPointAddressId。这工作,并允许我在结果模型

中做SupplyPoint.SupplyPointAddress

答案 1 :(得分:1)

因为您正在使用真正的数据库进行测试。使用一些

代码优先的数据库初始化策略:

public class SchoolDBContext: DbContext 
{

    public SchoolDBContext(): base("SchoolDBConnectionString") 
    {
        Database.SetInitializer<SchoolDBContext>(new CreateDatabaseIfNotExists<SchoolDBContext>());

        //Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseIfModelChanges<SchoolDBContext>());
        //Database.SetInitializer<SchoolDBContext>(new DropCreateDatabaseAlways<SchoolDBContext>());
        //Database.SetInitializer<SchoolDBContext>(new SchoolDBInitializer());
    }
    public DbSet<Student> Students { get; set; }
    public DbSet<Standard> Standards { get; set; }
}

(摘录自this site

这是非常自我解释的 如果已经创建了一个数据库,那么它只是DROP

快乐的编码!