如何使用实体框架7处理代码第一种方法这两种模式? [英] How handle these two models in code first approach using Entity Framework 7?

查看:174
本文介绍了如何使用实体框架7处理代码第一种方法这两种模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编码概念ASP.NET 5 MVC6将模拟库的证据。我使用CTP6和beta3版。

I am coding proof of concept ASP.NET 5 MVC6 that will simulate a library. I am using CTP6 and beta3.

使用代码第一种方法如何定义自己的实体框架7的关系?

How to define their relationship in Entity Framework 7 using code first approach?

架子和书籍需要一个连接表。在我看来,这个应用程序将考虑一次在一个架子和自我的书籍。换言之,一个货架具有一个与书一对多关系。

Shelf and Books need a join table. As I see it, this application will consider one shelf at a time and the books on that self. In other words, one shelf has a one to many relationship with books.

架型号:

public class Shelf
{
    public int ShelfId { get; set; }

    public string ShelfName { get; set; }

    public ShelfType MyProperty { get; set; }

    public virtual List<Book> Books { get; set; }

    public DateTime Created { get; set; }

    public string CreatedBy { get; set; }

    public DateTime Updated { get; set; }

    public string UpdatedBy { get; set; }

}

图书模型:

public class Book
{
    public int BookId { get; set; }

    public string BookName { get; set; }

    public string BookAuthor { get; set; }

    public string BookGenre { get; set; }

    public DateTime BookPublishedDate { get; set; }

    public string Description { get; set; }

    public DateTime Created { get; set; }

    public string CreatedBy { get; set; }

    public DateTime Updated { get; set; }

    public string UpdatedBy { get; set; }

}



目标



我想实现像在ASP .NET 5.由于TPH和其他功能没有在EF7还没有实现,我在正确的轨道上吗?

Goal

I would like to implement something like this in ASP.NET 5. Since TPH and other features are not yet implemented in EF7, am I on the right track?

在换句话说,我想到的是以下内容:

In other words, I am thinking the following:


  • ShelfBookViewModel(后我所需要的图模型,以显示一个架和书籍,或书,并能够nagivate回搁板)。我可能只是将其命名为ShelfBook。这似乎是错误的做法。

  • 我宁愿EF7能够找出我有一个加入和单独的担忧创造的ViewModels。否则,这是违反可靠的代码的做法。类似如下(从链接更早)。

  • 加入表没有在EF7实施还和我问徒劳?

我一直没能得到以下如下工作:

I have not been able to get the following to work as shown below:

 modelBuilder.Entity<Shelf>() 
            .HasMany(p => p.Shelfs) 
            .WithMany(t => t.Books) 
            .Map(mc => 
               { 
                   mc.ToTable("ShelfJoinBook"); 
                   mc.MapLeftKey("ShelfId"); 
                   mc.MapRightKey("BookId"); 
               });

感谢您的阅读我的问题的时间。我希望别人觉得这非常有用。

Thank you for your time in reading my question. I hope others find this useful.

推荐答案

要回答你的问题对多到多,你应该定义你的映射如下(和必然,更新模型类):

To answer your question about many-to-many, you should define your mapping as follows (and, necessarily, update your model classes):

modelBuilder.Entity<Shelf>() 
    .HasMany(p => p.Books) 
    .WithMany(t => t.Shelves) // doesn't exist 
    .Map(mc => 
       { 
           mc.ToTable("BookShelves"); 
           mc.MapLeftKey("ShelfId"); 
           mc.MapRightKey("BookId"); 
       });



这将要求你加一个的ICollection<保质>货架{搞定;组; } 图书。但是,如果你这样做你会那么有以下(这是我的 MCVE 的版本的示例):

which would require you to add an ICollection<Shelf> Shelves { get; set; } to Book. But, if you did so you'd then have the following (this is my MCVE version of your example):

public class Shelf
{
    public int ShelfId { get; set; }
    public virtual ICollection<Book> Books { get; set; }
}

public class Book
{
    public int BookId { get; set; }
    public virtual ICollection<Shelf> Shelves { get; set; }
}



然后你不再需要的映射无论如何,因为EF会。产生一切为您约定关系,包括连接表,只会在数据库本身是可见的。

and then you'd no longer need the mapping anyway, since EF would generate all the relations for you by convention, including the join table that would be visible only in the database itself.

不过与ndash的;从您的实际模型类,并将其与ndash的在你的讨论;看来你真的只需要一到多,在这种情况下,你应该接受Pynt的回答,而忽略我的。

However – from your actual model classes and your discussion around them – it appears you really only need one-to-many, in which case, you should accept Pynt's answer, and ignore mine.

修改

正如格特·阿诺德在的意见,EF7指出,目前不(不再?)支持生成的连接表,这意味着我们将不得不推出自己的,这是的可能更好反正

As pointed out by Gert Arnold in comments, EF7 does not currently (no longer?) supports generated join tables, which means we'll have to roll our own, which is probably better anyway.

下面是一个方式,我认为我可以做我的自己的,简单的,从现在开始连接的表,如果EF将不再做这些,我(警告&MDASH;我只是想关闭这个问题......我没有打扰尝试这在编译器中循环,所以YMMV):

Here's a way I think I might do my own, simple, join tables from now on, if EF will no longer do them for me (warning — I'm just trying to close the loop on this issue ... I'm not bothering to try this in a compiler, so YMMV):

public class Shelf
{
    public int ShelfId { get; set; }
    public virtual ICollection<BookShelf> BookShelves { get; set; }
    public virtual IQueryable<Book> Books
    {
        get
        {
            return BookShelves.Where(s => s.ShelfId == ShelfId)
                              .Select(s => s.Book);
        }
    }
}

public class Book
{
    public int BookId { get; set; }
    public virtual ICollection<BookShelf> BookShelves { get; set; }
    public virtual IQueryable<Shelf> Shelves
    {
        get
        {
            return BookShelves.Where(s => s.BookId == BookId)
                              .Select(s => s.Shelf);
        }
    }
}

public class BookShelf
{
    [Key]
    public int BookId { get; set; }
    [ForeignKey("BookId")]
    public Book Book { get; set; }
    [Key]
    public int ShelfId { get; set; }
    [ForeignKey("ShelfId")]
    public Shelf Shelf { get; set; }
}

这篇关于如何使用实体框架7处理代码第一种方法这两种模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆