【问题标题】:One to zero or one relationship with Code first c#与 Code first c# 的一对零或一对关系
【发布时间】:2018-04-13 22:09:53
【问题描述】:

我正在尝试学习使用 Code First。在图 1 中,您可以看到我想从 Code First 应用程序中变成的 EER 模型。

现在我尝试从我的应用程序中获得相同的结果。您可以在下面看到我从我的应用程序中成功生成的 EER 模型(在 MySQL Workbench 中使用逆向工程)。

如您所见,在表“属性”和“地面”之间创建一对零或一对关系时存在问题。

我有一个抽象的 EntityBase 类

public abstract class EntityBase
{
    public abstract int Id { get; set; }
}

也是一个继承EntityBase类的GenericRepository类

public class GenericRepository<T> : IRepository<T> where T : EntityBase

继承 DbContext 类的 MapDBContext 类。在这个类中,您可以看到 OnModelCreating 方法是“Override”。在该方法中,我尝试配置“Properties”和“Grounds”表之间的关系。

public class MapDBContext : DbContext
{
    public virtual DbSet<Agreements> Agreements { get; set; }
    public virtual DbSet<BuyersRenters> BuyersRenters { get; set; }
    public virtual DbSet<Properties> Properties { get; set; }
    public virtual DbSet<Grounds> Grounds { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Grounds>().HasOptional(s => s.Properties).WithRequired(lu => lu.Grounds);
        base.OnModelCreating(modelBuilder);
    }

    public MapDBContext(string connectionString) : base(connectionString)
    {

    }
}

以下是“Properties”和“Grounds”表的两个 Code First 类(注意:Properties 类是抽象的):

[Table("eigendommen")]
public abstract class Properties : EntityBase
{
    public override int Id { get; set; }
    [Column("gemeente")]
    [Required]
    public string Town { get; set; }
    [Column("straat")]
    [Required]
    public string Street { get; set; }

    public virtual List<Agreements> Agreements { get; set; }
    public virtual Grounds Grounds { get; set; }
}



[Table("gronden")]
public class Grounds : Properties
{
    [Key]
    public override int Id { get; set; }
    [Column("opp")]
    public double? Surface { get; set; }
    [Column("type")]
    [EnumDataType(typeof(TypeNames))]
    [Required]
    public TypeNames Types { get; set; }

    public virtual Properties Properties { get; set; }
}

有人可以帮我解决我做错了什么吗?我一直在寻找几个小时,尝试使用“必需”属性,使用“?”使其可以为空并具有“ForeignKey”属性。但是所有这些解决方案都会给出错误或与我现在拥有的类似的表格。

【问题讨论】:

  • 如果你有一个 1:1 的关系,有时可能是 1:0,那么你应该注意规则 4 和 5:删除该表。严格来说,您应该始终使用邮政编码作为地名的主键(因为它实际上是这样的)。但几乎每个数据库设计师都会本能地将两者都写成地址的一部分。
  • 这应该会有所帮助:entityframeworktutorial.net/code-first/…
  • 此链接可能对您有所帮助:entityframeworktutorial.net/code-first/…

标签: c# mysql entity-framework


【解决方案1】:

用代码优先的c#定义一对一和零

如果您想为学生提供一个或零个地址。

您可以按照下面的代码进行操作

public class Student
{
    public Student() { }
    public int StudentId { get; set; }
    public string StudentName { get; set; }
    public virtual StudentAddress StudentAddress { get; set; }

}

public class StudentAddress
{
    [ForeignKey("Student")]
    public int StudentAddressId { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public int Zipcode { get; set; }
    public string State { get; set; }
    public string Country { get; set; }

    public virtual Student Student { get; set; }
}

您必须在 DbContext 中定义 OnModelCreating,然后定义学生和学生地址之间的关系。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Configure the primary key for the StudentAddresses
    modelBuilder.Entity<StudentAddress>()
        .HasKey(t => t.StudentAddressId);

    // Map one-to-zero or one relationship
    modelBuilder.Entity<StudentAddress>()
        .HasRequired(t => t.Student)
        .WithOptional(t => t.StudentAddress);

 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多