code首先,与工作的关系,播种 [英] Code first, working with relationships, seeding

查看:132
本文介绍了code首先,与工作的关系,播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想简单地创建一个已经存在两个实体之间的关系。

I would like to simply create a relationship between two entities that already exists..

在我的情况我有可能有多个流派(也就是一堆的实体)的专辑实体

In my case I have an Album entity which might have multiple Genres(which also is a bunch of entities)

我的相册样板看起来是这样的:

My Album model looks like this:

public class AlbumModel : IModel
{
    public List<GenreModel> Genres { get; set; }
    public ArtistModel Artist { get; set; }
    public Guid Id { get; set; }
    public string Title { get; set; }
    public float Price { get; set; }
    public string ArtUrl { get; set; }
}

然后我想一个相册样板对象和一堆GenreModel对象之间建立的关系船/实体。

I then want to create a relation ship between a Album model-object and a bunch of GenreModel-objects/entities..

在正常的实体框架我只是分配一个新的EntityKey ..但是我不太清楚如何先做到这一点使用code ..
我还注意到,有些人有一个名为xxxxId一个额外的属性,他们希望创造之间的参考实体..然后简单地分配一个值到一些如何神奇地创造..实体之间的参考xxxxId财产,我想这对于一对一或一对多的关系正常工作..但我想,对于很多不工作的许多relationship..or?

In the "normal" entity framework I would just assign a new EntityKey.. but Im not quite sure of how to do this using code first.. I also noticed that some people has an extra property called xxxxId for the entity they want to create a reference between.. and then simply assign a value to the xxxxId property which some how magically creates the reference between the entities.. and I guess this works fine for a one to one or a one to many relationship.. but I guess that doesnt work for a many to many relationship..or?

反正..这是我的GenresModel:

Anyway.. this is my GenresModel:

public class GenreModel : IModel
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public List<AlbumModel> Albums { get; set; }
}

这是我尝试过..但是这不会创造relationship..it只会简单地在我的数据库创建一个额外的实体/行..

And this is what I tried before.. but this wont create the relationship..it will only simply create an additional entity/row in my database..

var artistRepository = new ArtistRepository();
        var genresRepository = new GenreRepository();

        #region Lordi
        context.Albums.Add
            (
                new AlbumModel
                {
                    Artist = artistRepository.GetByName("Lordi"),
                    ArtUrl = "http://upload.wikimedia.org/wikipedia/en/8/8f/The_Monster_Show.jpg",
                    Genres = new List<GenreModel> { genresRepository.GetByName("Rock"), genresRepository.GetByName("Hard rock") },
                    Id = Guid.NewGuid(),
                    Price = 3,
                    Title = "The Monster Show",
                }
            );
        context.Albums.Add
            (
                new AlbumModel
                {
                    Artist = artistRepository.GetByName("Lordi"),
                    ArtUrl = "http://www.nuclearblast.de/static/articles/157/157892.jpg/1000x1000.jpg",
                    Genres = new List<GenreModel> { genresRepository.GetByName("Rock"), genresRepository.GetByName("Hard rock") },
                    Id = Guid.NewGuid(),
                    Price = 10,
                    Title = "Zombilation - The Greatest Cuts",
                }
            );
        #endregion

...并备案...没有我不听洛尔迪:) ..只是虚拟数据..和一些奇怪的怪异原因洛尔迪是排在脑海的第一个乐队。

...and for the record... no I dont listen to Lordi :).. just dummy data.. and for some wierd spooky reason Lordi was the first band that came in mind..

在此先感谢!

推荐答案

指定的集合属性为你做会自动创建一个多到很多所谓的联表 GenreModelAlbumModels

Specifying those collection properties as you have done will automatically create a many-to-many linking table called GenreModelAlbumModels.

不过,我怀疑你的资料库是明知有关系preventing EF。如果你不加载在同一个数据上下文中的实体GenreModel,它可能不知道,它们之间的联系。作为一个例子,这个code工作:

However, I suspect that your repository is preventing EF from knowing that there is a relationship. If you're not loading the GenreModel entities in the same data context, it may not know that there is a link between them. As an example, this code works:

    using (var context = new DataContext())
    {
        #region Lordi

        context.Albums.Add
            (
                new AlbumModel
                {
                    ArtUrl = "http://upload.wikimedia.org/wikipedia/en/8/8f/The_Monster_Show.jpg",
                    Genres = new List<GenreModel> { context.Genres.First(i => i.Name == "Rock"), context.Genres.First(i => i.Name == "Hard Rock") },
                    Id = Guid.NewGuid(),
                    Price = 3,
                    Title = "The Monster Show",
                }
            );

        context.SaveChanges();

        #endregion
    }

该GenreModels连接到我们正在加入到AlbumModels同样的背景下,使EF知道有实体之间的关系。

The GenreModels are connected to the same context that we're adding the AlbumModels to, so EF knows that there is a relationship between the entities.

要确保它的正常工作,请在您的数据库链接该表。它会从你在code被隐藏。

To make sure it's working properly, look for that linking table in your DB. It will be hidden from you in the code.

这篇关于code首先,与工作的关系,播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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