1

我在 EF v4 中遇到了关系问题。我知道这个问题很受欢迎,我试图在 Internet 上找到一些东西,但没有帮助。我有一个与地址一对一的类人(人有地址)

class Person
{
public int PersonId{get;set;}
public string FisrtName{get; set;}
...
public int AddressId{get;set;}
public virtual Address Address{get;set;}
}
class Address
{
public int AddressId{get;set}
public string Street{get;set;}
...
}

我假设这是一对一的关系,我遵循 EF v4 中的所有约定。但是当我创建生成的数据库的数据库图时。我看不到 Person 和 Address 之间的任何关系。我的意思是我看到两个表没有关系,Person 上的键和 Address 上的键另一个具有这种关系的表创建一对多帐户上的键和地址上的无穷大,但代码相同。我在设计器中只看到一对多的关系,在某些情况下,我可以看到表之间必须存在的任何关系。请帮帮我!谢谢你的帮助 PS我认为添加表格时设计师有问题

4

2 回答 2

4

如果您首先使用代码,则可以使用流畅的代码 API 或使用属性来优化模型来自定义持久性映射。如果您要使用简单的键名(例如 Id),EF 可以通过推理解决关系;在您的情况下,EF 需要提示 PersonID 和 AddressID 是键。

要使用属性方法,请在项目中添加对 System.ComponentModel.DataAnnotations 的引用以及相应的“使用 System.ComponentModel.DataAnnotations;” 根据需要在源文件中。以下示例 (EF 4.3.1) 将在生成的 Addresses 和 Persons 表之间生成“一对多”关系(在这种情况下您不需要一对一)。运行代码后,您将在 SQL Server 数据库图表窗口中看到关系。

class Program
{
    static void Main(string[] args)
    {
        using (ContactsEntities entities = new ContactsEntities())
        {
            Address doeaddress = new Address() { Street = "1 Broadway", ZipCode = "01234" };
            Address doeaddress2 = new Address() { Street = "2 Broadway", ZipCode = "01234" };
            entities.Addresses.Add(doeaddress);
            entities.Persons.Add(new Person() { FirstName = "Jane", LastName = "Doe", Address = doeaddress });
            entities.Persons.Add(new Person() { FirstName = "John", LastName = "Doe", Address = doeaddress });
            entities.Persons.Add(new Person() { FirstName = "Jim", LastName = "Doe", Address = doeaddress2 });
            entities.SaveChanges(); 
        }
        Console.WriteLine("Press Enter to exit...");
        Console.ReadLine();

    }
}

[Table("Addresses")]
public partial class Address
{
    public Address()
    {
        this.Persons = new HashSet<Person>();
    }
    [Key]
    public int AddressID { get; set; }
    [Required]
    public string Street { get; set; }
    [RegularExpression(@"^(\d{5}-\d{4}|\d{5}|\d{9})$")]
    [Required]
    public string ZipCode { get; set; }

    public virtual ICollection<Person> Persons { get; set; }
}

[Table("Persons")]
public partial class Person
{
    [Key]
    public int PersonID { get; set; }
    public int AddressID { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [ForeignKey("AddressID")]
    public virtual Address Address { get; set; }
}

public partial class ContactsEntities : DbContext
{
    public DbSet<Address> Addresses { get; set; }
    public DbSet<Person> Persons { get; set; }
}
于 2012-05-12T21:29:56.650 回答
0

根据反馈,这里是一对一(实际上是一对零或一)关系的示例。我使用 fluent API 来设置 OnModelCreating 覆盖中的关系。在这种情况下,一个人最多可以拥有一个照片行。在实践中,如果 Photos 表包含一个或多个大字节数组来保存图像数据,这可能很有用;为了清楚起见,我使用字符串来表示图像的链接。

    static void Main(string[] args)
    {
        using (ContactsEntities entities = new ContactsEntities())
        {

            entities.Persons.Add(new Person() { FirstName = "Jane", LastName = "Doe", Photo = new Photo() { PhotoLink = "/images/jane.jpg" } });
            entities.Persons.Add(new Person() { FirstName = "John", LastName = "Doe" }); // no photo
            entities.Persons.Add(new Person() { FirstName = "Joe", LastName = "Smith", Photo = new Photo() { PhotoLink = "/images/joe.jpg", ThumnbnailLink = "/images/thumbs/joe.jpg" } });

            // note that the following is not allowed based on the defined RI rules - will fail on call to SaveChanges:
            // entities.Photos.Add(new Photo() { PhotoLink = "/images/as.jpg" });

            entities.SaveChanges();

            foreach (Person person in entities.Persons)
            {
                Console.WriteLine("{0} {1} {2}", person.FirstName, person.LastName, person.Photo == null ? "missing photo" : person.Photo.PhotoLink);
            } 
        }

        Console.WriteLine("Press Enter to exit...");
        Console.ReadLine();

    }
}

public partial class ContactsEntities : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // a Person may have at most one Photo
        modelBuilder.Entity<Person>().HasOptional<Photo>(p => p.Photo);
        // a Photo is dependant on a Person (non-nullable FK constraint)
        modelBuilder.Entity<Photo>().HasRequired<Person>(p => p.Person);
        base.OnModelCreating(modelBuilder);
    }
    public DbSet<Photo> Photos { get; set; }
    public DbSet<Person> Persons { get; set; }
}

[Table("Photos")]
public partial class Photo
{
    [Key]
    public int PhotoID { get; set; }
    [Required]
    public string PhotoLink { get; set; }
    public string ThumnbnailLink { get; set; }
    public virtual Person Person { get; set; }
}

[Table("Persons")]
public partial class Person
{
    [Key]
    public int PersonID { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }

    public virtual Photo Photo { get; set; }
}
于 2012-05-13T15:22:31.120 回答