与ASP.NET MVC身份建立一对多关系时出错

时间:2019-06-17 20:36:30

标签: c# asp.net-mvc entity-framework asp.net-identity

我正在使用asp.net mvc身份。我正在尝试在applicationuser和Blog之间建立关系。但是有一些我想念的东西,而且我遇到了错误。

这是Blog实体类:

public class Blog
    {

        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public string Img { get; set; }
        public bool IsApproved { get; set; }
        public int ReadingCount { get; set; }
        public int CategoryId { get; set; }
        [DataType(DataType.Date)]
        public DateTime EnrollmentDate { get; set; }
        public Category Category { get; set; }             
        public List<Comment> Comments { get; set; }
        public string UserId { get; set; }
        public ApplicationUser ApplicationUser { get; set; }

    }

和博客模型:

public class BlogModel
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public string Img { get; set; }
        public bool IsApproved { get; set; }
        public int ReadingCount { get; set; }
        public int CategoryId { get; set; }
        public string Category { get; set; }
        public string isim { get; set; }
        public string user { get; set; }        // about user
        public DateTime EnrollmentDate { get; set; }
        public List<CommentModel> Comments { get; set; }

    }

和applicationUser身份:

public class ApplicationUser: IdentityUser
    {

        public string Name { get; set; }
        public string Surname { get; set; }
        public string Yenialan { get; set; }
        public List<Blog> Bloglar { get; set; }

    }

和applicationUser的模型

public class userModel
    {
        public string Name { get; set; }        
        public List<BlogModel> Bloglar { get; set; }
    }

DataContext:

public class DataContext: DbContext
    {
        public DataContext() :base("dataConnection")
        {

        }
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Comment> Comments { get; set; }
        public DbSet<ApplicationUser> ApplicationUsers { get; set; }
    }

到目前为止,一切都很完美。用户和角色表组成。它还由博客表组成。(我指的是sql表) 但是运行家庭控制器时出现错误。

运行代码如下:

public class HomeController : Controller
    {
        DataContext context = new DataContext();        
        public ActionResult Index()
        {

            var bloglar = context.Blogs
                .Where(i => i.IsApproved == true)
                .Select(i => new BlogModel()
                {
                    Id = i.Id,
                    Title = i.Title,
                    Img = i.Img,
                    EnrollmentDate = i.EnrollmentDate,
                    CategoryId = i.CategoryId,
                    Category = i.Category.Name,               
                    user = i.ApplicationUser.Name
                }).ToList();


            return View(bloglar);
        }
}

这是错误:

System.Data.Entity.ModelConfiguration.ModelValidationException: 'One or more validation errors were detected during model generation:

BlogApp.Entity.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType.
BlogApp.Entity.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType.
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.
'

我在互联网上做了很多搜索以查找此错误。我发现的解决方案不适用于我。您认为该错误的解决方案是什么? 预先感谢您的帮助。

0 个答案:

没有答案