EF 代码第一个错误“实体集已定义”
我使用流畅的语法来配置简单的 Movie
和 Genre
实体之间的 M:M 关系。工作正常。
我的问题是我还希望将链接/连接表 MovieGenre
作为实体公开。我意识到这是没有必要的,因为它只包含密钥,但我希望拥有它,因为它会让一些操作更容易。
当我尝试配置链接表时出现此错误:
具有架构“Media”和表“MovieGenre”的实体集“MovieGenre1” 已经定义了。
我知道这与我已经建立了 M:M 关系有关,但还没有弄清楚如何让它们一起工作。
以下是我的代码:
public class Genre
{
public int GenreID { get; set;}
public string Name { get; set; }
public ICollection<Movie> Movies { get; set; }
}
public class Movie
{
public int MovieID { get; set; }
public string Title { get; set; }
public ICollection<Genre> Genres { get; set; }
}
public class MovieGenre
{
public int MovieID { get; set; }
public int GenreID { get; set; }
public Movie Movie { get; set; }
public Genre Genre { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Genre
modelBuilder.Entity<Genre>()
.ToTable("Genre", "Media");
//Movie
var movieCfg = modelBuilder.Entity<Movie>();
movieCfg.ToTable("Movie", "Media");
movieCfg.HasMany(m => m.Genres)
.WithMany(g => g.Movies)
.Map(m =>
{
m.MapLeftKey("MovieID");
m.MapRightKey("GenreID");
m.ToTable("MovieGenre", "Media");
});
//MovieGenres
var movieGenresCfg = modelBuilder.Entity<MovieGenre>();
movieGenresCfg.ToTable("MovieGenre", "Media");
movieGenresCfg.HasKey(m => new
{
m.MovieID, m.GenreID
});
base.OnModelCreating(modelBuilder);
}
任何帮助将不胜感激。谢谢。
I'm using fluent syntax to configure a M:M relationship between simple Movie
and Genre
entities. That's working OK.
My problem is that I also wish to expose the link/join table MovieGenre
as an entity as well. I realize it's not necessary since it only contains keys, but I wish to have it because it will make some operations easier.
I get this error when I try to configure the link table:
The EntitySet 'MovieGenre1' with schema 'Media' and table 'MovieGenre'
was already defined.
I know it has to do w/ me already setting up the M:M relationship but haven't figured out how to make it all work together.
Below is my code:
public class Genre
{
public int GenreID { get; set;}
public string Name { get; set; }
public ICollection<Movie> Movies { get; set; }
}
public class Movie
{
public int MovieID { get; set; }
public string Title { get; set; }
public ICollection<Genre> Genres { get; set; }
}
public class MovieGenre
{
public int MovieID { get; set; }
public int GenreID { get; set; }
public Movie Movie { get; set; }
public Genre Genre { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Genre
modelBuilder.Entity<Genre>()
.ToTable("Genre", "Media");
//Movie
var movieCfg = modelBuilder.Entity<Movie>();
movieCfg.ToTable("Movie", "Media");
movieCfg.HasMany(m => m.Genres)
.WithMany(g => g.Movies)
.Map(m =>
{
m.MapLeftKey("MovieID");
m.MapRightKey("GenreID");
m.ToTable("MovieGenre", "Media");
});
//MovieGenres
var movieGenresCfg = modelBuilder.Entity<MovieGenre>();
movieGenresCfg.ToTable("MovieGenre", "Media");
movieGenresCfg.HasKey(m => new
{
m.MovieID, m.GenreID
});
base.OnModelCreating(modelBuilder);
}
Any help would be appreciated. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你正在尝试的是不可能的。您需要从映射中删除您的 MovieGenre。
不必担心从数据库中获取实体只是将其添加到流派列表中。如果您多次通过 id 加载一个实体,则无论如何只有一次对数据库的调用。随着 EF 变得更好并添加二级缓存,它将正常工作并表现良好。如果你过于强制,那么框架就失去了魔力。
如果您考虑模型的域和用户,那么大多数模型都是读取的,因此没有太多更新。最重要的是,将电影添加到流派或其他方式可能意味着很多事情,在这种情况下,您不应该直接公开集合,而是添加方法来实现行为。
行为的一个例子是“如果您将一部电影添加到超过 10 种类型,那么该电影应该将其类别更改为 X”以及各种其他内容。不要只考虑数据。 领域驱动设计是一本关于这个主题的好书。
在您的例子中,您可以将电影实体视为根聚合,因为电影是您的用户所追求的,而电影是您要作为管理员编辑的内容。也许一部电影可以没有流派而存在,所以流派只是电影上的一个标签,一个价值对象,并不是很重要。您的映射将变为:
查询将如下所示:
这几乎就是您要对域执行的所有操作。
What you are trying is not possible. You need to remove your MovieGenre from the mapping.
Do not worry about fetching the entity from the database just to add it to the list of Genres. If you load an entity by id multiple times there is only one call to the DB anyway. As EF gets better and a second level cache is added it will just work and perform well. If you are too imperative there is no magic left to the framework.
And if you think about the domain and the users of your model, most of it is reads anyway so not many updates. On top of it adding a movie to a genres or the other way around could mean many things in which case you should not expose the collections directly but add methods to to implement the behaviours.
One example of behaviour would be "if you add a movie to more than 10 genres then the movie should change the say its category to X" and all sorts of other things. Stop thinking about data only. Domain Driven Design is a good book to read on the subject.
In your case here you can consider the Movie entity as a root aggregate because a Movie is what your users are after and a Movie is what you are going to edit as an administrator. A movie can exist without a Genre, maybe, so genre is just a tag on the movies, a value object, and not very important. Your mappings would become:
The queries would look like:
This is pretty much all you are going to do with your domain.