0

I m working on an existing MVC project.When my context class inherited from DBContext everythink is fine but when i changed it to IdentityDbContext i get that error.The error occured in that line : var makaleler = context.Makale.ToList();

my controller:

using mvcblogdeneme.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace mvcblogdeneme.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        BlogDatabaseContext context = new BlogDatabaseContext();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult CategoryWidgetGetir()
        {
            var kat = context.Kategori.ToList();
            return View(kat);
        }

        public ActionResult TumMakalelerGetir()
        {
            var makaleler = context.Makale.ToList();
            return View("MakaleListele", makaleler);//makalelistele bi partial view
        }

    }
}

this is my model:

namespace mvcblogdeneme.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("Makale")]
    public partial class Makale
    {

        public int Id { get; set; }

        [Required]
        [StringLength(150)]
        public string Baslik { get; set; }

        [Required]
        public string Icerik { get; set; }

        public DateTime YayimTarihi { get; set; }

        public int KategoriID { get; set; }

        public virtual Kategori Kategori { get; set; }
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
bthn
  • 176
  • 2
  • 13

1 Answers1

0

If the exception message shows like this and DbContext is being used:

EntityType '[[table name]]' has no key defined. Define the key for this EntityType.

EntityType: EntitySet '[[entity name]]' is based on type '[[table name]]' that has no keys defined.

Then definitely you need to use KeyAttribute on Id property to mark it as primary key field inside table model class and problem will solved:

[Table("Makale")]
public partial class Makale
{
    // if the ID also set as auto-increment, uncomment DatabaseGenerated attribute given below
    // [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int Id { get; set; }

    [Required]
    [StringLength(150)]
    public string Baslik { get; set; }

    [Required]
    public string Icerik { get; set; }

    public DateTime YayimTarihi { get; set; }

    public int KategoriID { get; set; }

    public virtual Kategori Kategori { get; set; }
}

However, if the cause is found out coming from IdentityDbContext and EF Code First being used, you need to do these steps (credits to DotNetHaggis):

  1. Create configuration class for IdentityUserLogin & IdentityUserRole:

    // taken from /a/20912641
    public class IdentityUserLoginConfiguration : EntityTypeConfiguration<IdentityUserLogin>
    {
    
        public IdentityUserLoginConfiguration()
        {
            HasKey(iul => iul.UserId);
        }
    
    }
    
    public class IdentityUserRoleConfiguration : EntityTypeConfiguration<IdentityUserRole>
    {
        public IdentityUserRoleConfiguration()
        {
            HasKey(iur => iur.RoleId);
        }
    
    }
    
  2. Add those two configurations above inside OnModelCreating method:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new IdentityUserLoginConfiguration());
        modelBuilder.Configurations.Add(new IdentityUserRoleConfiguration());
    }
    

At this point, the error should get resolved due to primary key for every identity tables has been set.

Similar issues:

An exception of type 'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll

Merge MyDbContext with IdentityDbContext

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61