首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实体框架6:引用UserProfile

实体框架6:引用UserProfile
EN

Stack Overflow用户
提问于 2016-05-28 09:59:25
回答 1查看 54关注 0票数 0

我试图用自定义DBInitializer编写一个ASP.MVC应用程序,但我遇到了错误:

附加信息:元数据集合中不存在带有标识'MailMail.Models.MailDbContext.Video_UserProfile‘的成员。

我有录像课:

代码语言:javascript
复制
public class Video : Entity
{
    public string Name { get; set; }
    public string Path { get; set; }
    public virtual UserProfile UserProfile { get; set; }
}

我的DBInitializer:

代码语言:javascript
复制
 public class MailDbInitializer : DropCreateDatabaseAlways<MailDbContext>
    {
        protected override void Seed(MailDbContext context)
        {
            UsersContext userContext = new UsersContext();

            if (!WebMatrix.WebData.WebSecurity.Initialized)
            {
                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }


            var mongol1 = new { Email="mail2@mail", UserName="Tom"};
            var mongol2 = new { Email = "mail@mail", UserName = "Karola" };
            WebSecurity.CreateUserAndAccount(mongol1.UserName, "secret", mongol1);

            Task<UserProfile> user1Task = userContext.UserProfiles.SingleOrDefaultAsync(o => o.UserName == mongol1.UserName);
            user1Task.Wait();
            UserProfile user1 = user1Task.Result;

            WebSecurity.CreateUserAndAccount(mongol2.UserName, "123456", mongol2);
            Task<UserProfile> user2Task = userContext.UserProfiles.SingleOrDefaultAsync(o => o.UserName == mongol2.UserName);
            user2Task.Wait();
            UserProfile user2 = user1Task.Result; 



            Video video1 = new Video() {Name="Moje wideo", UserProfile=user1, Path = "~/App_Data/"+user1.UserId+"/Video1"};
            Video video2 = new Video() { Name = "Moje wideo", UserProfile = user1, Path = "~/App_Data/" + user1.UserId + "/Video2" };
            Video video3 = new Video() { Name = "Moje wideo", UserProfile = user2, Path = "~/App_Data/" + user2.UserId + "/Video3" };
            Video video4 = new Video() { Name = "Moje wideo", UserProfile = user2, Path = "~/App_Data/" + user2.UserId + "/Video4" };

            context.Videos.Add(video1);  //HERE ocures the error!
            context.Videos.Add(video2);

我认为这可能与不同的背景有关。我说的对吗?

EN

回答 1

Stack Overflow用户

发布于 2016-05-29 10:10:11

问题是我有两个不同的上下文,它们都指向同一个数据库。我的解决办法是:我刚刚复制了

代码语言:javascript
复制
 public DbSet<UserProfile> UserProfiles { get; set; }

我主要的DBContext课程。解决了问题:-)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37497611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档