繁体   English   中英

实体框架中与一种类型的一对一关系

[英]One to one relationships with one type in entity framework

我有我的主类: Person和一些其他类: AddressInfoDocumentInfo 如何配置我的关系,使其像具有2个地址和3个文档的“ Person一样工作? 它不是数组。 它被称为链接。

看起来像:

public class Person
{
    public int Id {get;set;}
    public virtual AddressInfo RegistrationAddress {get;set;}
    public virtual AddressInfo ResidenceAddress {get;set;}
}
public class AdressInfo
{
    public int Id {get;set;}
    public virtual Person Person {get;set;}
}

DocumentInfo相同。 它甚至不可能接近正确的解决方案。

尝试这个。 它对我有用。 我没有设置属性,但是代码优先API对我来说更好



namespace OneToOne
{
    class Program
    {
        static void Main(string[] args)
        {

            using (var db = new MyContext())
            {

                var person = new Person();


                db.Persons.Add(person);
                db.SaveChanges();


                person = db.Persons.Include("RegistrationAddress").Include("ResidenceAddress").FirstOrDefault();

                var address = new AdressInfo { Person = person };
                person.RegistrationAddress = address;
                person.ResidenceAddress = address;

                db.Persons.Attach(person);
                db.SaveChanges();


            }
        }
    }

    public class MyContext : DbContext
    {
        public DbSet Persons { get; set; }
        public DbSet AdressInfos { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new AdressInfoConfig());
            modelBuilder.Configurations.Add(new PersonConfig());
        }

    }

    public class PersonConfig : EntityTypeConfiguration
    {

        public PersonConfig()
        {
            this.ToTable("Persons");
            this.HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        }

    }

    public class Person
    {
        public int Id { get; set; }

        public virtual AdressInfo RegistrationAddress { get; set; }
        public virtual AdressInfo ResidenceAddress { get; set; }
    }

    public class AdressInfo
    {
        public int Id { get; set; }
        public virtual Person Person { get; set; }
    }

    public class AdressInfoConfig : EntityTypeConfiguration
    {

        public AdressInfoConfig()
        {
            this.ToTable("AdressInfos");
            this.HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.HasOptional(x => x.Person).WithOptionalDependent(x => x.RegistrationAddress).WillCascadeOnDelete(false);
            this.HasOptional(x => x.Person).WithOptionalDependent(x => x.ResidenceAddress).WillCascadeOnDelete(false);
        }


    }
}

尝试了您的解决方案。 多数民众赞成在我的模型:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime BirthDate { get; set; }
    public string IdentificationNumber { get; set; }
    public string HomePhone { get; set; }
    public string MobilePhone { get; set; }
    public string Email { get; set; }
    public bool DeadHead { get; set; }
    public virtual PassportInfo Passport { get; set; }
    public virtual PassportInfo DriverLicense { get; set; }
    public virtual PassportInfo PensionCertificate { get; set; }
    public virtual AddressInfo ResidenseAddress { get; set; }
    public virtual AddressInfo RegistrationAddress { get; set; }
}

public class PassportInfo
{
    public int Id { get; set; }
    public string Series { get; set; }
    public int Number { get; set; }
    public string IssuedBy { get; set; }
    public DateTime IssueDate { get; set; }



    public virtual Person Person { get; set; }
}



public class AddressInfo
{
    public int Id { get; set; }
    public string Index { get; set; }
    public string Region { get; set; }
    public string District { get; set; }
    public string Street { get; set; }
    public string Locality { get; set; }
    public int House { get; set; }
    public int Apartment { get; set; }

    public int? StreetTypeId { get; set; }
    public StreetType StreetType { get; set; }

    public int? LocalityTypeId { get; set; }
    public LocalityType LocalityType { get; set; }


    public virtual Person Person { get; set; }

}

`那就是我的上下文配置:

public class InsuranceContext : DbContext
    {
        public DbSet<Person> People { get; set; }
        public DbSet<AddressInfo> AddressInfos { get; set; }
        public DbSet<PassportInfo> PassportInfos { get; set; }
        public DbSet<LocalityType> LocalityTypes { get; set; }
        public DbSet<StreetType> StreetTypes { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Configurations.Add(new AddressInfoConfig());
            modelBuilder.Configurations.Add(new PersonConfig());
            modelBuilder.Configurations.Add(new PassportInfoConfig());
        }
    }

    public class PersonConfig : EntityTypeConfiguration<Person>
    {
        public PersonConfig()
        {
            this.ToTable("People");
            this.HasKey(x => x.Id)
                .Property(x => x.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        }
    }

    public class AddressInfoConfig : EntityTypeConfiguration<AddressInfo>
    {
        public AddressInfoConfig()
        {
            this.ToTable("AddressInfos");
            this.HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.HasOptional(x => x.Person).WithOptionalDependent(x => x.RegistrationAddress).WillCascadeOnDelete(false);
            this.HasOptional(x => x.Person).WithOptionalDependent(x => x.ResidenseAddress).WillCascadeOnDelete(false);
        }
    }

    public class PassportInfoConfig : EntityTypeConfiguration<PassportInfo>
    {
        public PassportInfoConfig()
        {
            this.ToTable("PassportInfos");
            this.HasKey(x => x.Id).Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.HasOptional(x => x.Person).WithOptionalDependent(x => x.Passport).WillCascadeOnDelete(false);
            this.HasOptional(x => x.Person).WithOptionalDependent(x => x.DriverLicense).WillCascadeOnDelete(false);
            this.HasOptional(x => x.Person).WithOptionalDependent(x => x.PensionCertificate).WillCascadeOnDelete(false);
        }
    }

顺便说一句,它引发异常“参数@objname模棱两可,或者声明的@objtype(COLUMN)错误。” 当我尝试使用上下文或通过迁移更新基础时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM