EF Core Sqlite - FOREIGN KEY constraint failed migration - not nullable to nullable foreign key - sqlite

I want to change a foreign key on my table from not nullable to nullable
Class name : DestinatairePartageDocument
public class DestinatairePartageDocument
{
[Key]
public int DestinatairePartageDocumentId { get; set; }
//[ForeignKey("Document")]
//public int DocumentId { get; set; }
[ForeignKey("Document")]
public int? DocumentId { get; set; }
public Document Document { get; set; }
//[ForeignKey("Examen")]
[NotMapped]
public int? ExamenId { get; set; }
//public Examen Examen { get; set; }
[ForeignKey("DestinatairePartage")]
public int DestinatairePartageId { get; set; }
public DestinatairePartage DestinatairePartage { get; set; }
public string CodeAccesDocument { get; set; }
public DateTime DateFinValiditeCodeAccesDocument { get; set; }
public string TypePartage { get; set; } /* PartageClassique, PartageCodeGenere */
public string StatutDocument { get; set; }
public bool PartageActive { get; set; }
public DateTime DateCreation { get; set; }
public DateTime DateDerniereModification { get; set; }
}
Old property setup :
[ForeignKey("Document")]
public int DocumentId { get; set; }
public Document Document { get; set; }
New property setup :
[ForeignKey("Document")]
public int? DocumentId { get; set; }
public Document Document { get; set; }
Upon doing this update, I then update my migration schema :
dotnet ef Migrations add UpdateDocumentNullable
And then I apply the modification to the database :
dotnet ef database update
But I then have the following error "FOREIGN KEY constraint failed" :
Here is the migration file generated :
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_DestinatairesPartageDocuments_Documents_DocumentId",
table: "DestinatairesPartageDocuments");
migrationBuilder.AddColumn<string>(
name: "StatutPartageExamen",
table: "Examens",
type: "TEXT",
nullable: true);
migrationBuilder.AlterColumn<int>(
name: "DocumentId",
table: "DestinatairesPartageDocuments",
type: "INTEGER",
nullable: true,
oldClrType: typeof(int),
oldType: "INTEGER");
migrationBuilder.AddColumn<int>(
name: "ExamenId",
table: "DestinatairesPartageDocuments",
type: "INTEGER",
nullable: true);
migrationBuilder.AddForeignKey(
name: "FK_DestinatairesPartageDocuments_Documents_DocumentId",
table: "DestinatairesPartageDocuments",
column: "DocumentId",
principalTable: "Documents",
principalColumn: "DocumentId",
onDelete: ReferentialAction.Restrict);
}
Below are the queries created from the migration :
My DbContext class :
public class ApplicationDbContext: IdentityDbContext<ApplicationUser>, IDataProtectionKeyContext
{
private readonly ILoggerFactory _loggerFactory;
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, ILoggerFactory loggerFactory) : base(options)
{
_loggerFactory = loggerFactory;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(_loggerFactory);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
//modelBuilder.Entity<DestinatairePartageDocument>()
// .Property(dpd => dpd.DocumentId)
// .HasDefaultValueSql("NULL");
//modelBuilder.Entity<DestinatairePartageDocument>()
// .HasOne(dpd => dpd.Document)
// .WithMany()
// .OnDelete(DeleteBehavior.Cascade);
//modelBuilder.Entity<DestinatairePartageDocument>()
// .HasOne("PortailWorker.Models.Document", "Document")
// .WithMany()
// .HasForeignKey("DocumentId");
modelBuilder.Entity<DestinatairePartageDocument>()
.Property(ap => ap.DateCreation)
.HasDefaultValueSql("CURRENT_TIMESTAMP");
modelBuilder.Entity<DestinatairePartageDocument>()
.Property(ap => ap.DateDerniereModification)
.HasDefaultValueSql("CURRENT_TIMESTAMP");
modelBuilder.Entity<Revendeur>()
.HasMany(r => r.ListeClients)
.WithOne(c => c.Revendeur);
modelBuilder.Entity<ClientWPFFetcher>()
.HasKey(cwf => new { cwf.ClientId, cwf.WPFFetcherId });
modelBuilder.Entity<ClientWPFFetcher>()
.Property(cwf => cwf.RelationActivated)
.HasDefaultValue(true);
modelBuilder.Entity<ClientWPFFetcher>()
.HasOne(cwf => cwf.Client)
.WithMany(c => c.ClientWPFFetchers)
.HasForeignKey(cwl => cwl.ClientId);
modelBuilder.Entity<ClientWPFFetcher>()
.HasOne(cwf => cwf.WPFFetcher)
.WithMany(c => c.ClientWPFFetchers)
.HasForeignKey(cwl => cwl.WPFFetcherId);
modelBuilder.Entity<ClientWorkerLocal>()
.HasKey(cwl => new { cwl.ClientId, cwl.WorkerLocalId });
modelBuilder.Entity<ClientWorkerLocal>()
.Property(cwl => cwl.RelationActivated)
.HasDefaultValue(true);
modelBuilder.Entity<ClientWorkerLocal>()
.HasOne(cwl => cwl.Client)
.WithMany(c => c.ClientWorkersLocal)
.HasForeignKey(cwl => cwl.ClientId);
modelBuilder.Entity<ClientWorkerLocal>()
.HasOne(cwl => cwl.WorkerLocal)
.WithMany(c => c.ClientWorkersLocal)
.HasForeignKey(cwl => cwl.WorkerLocalId);
modelBuilder.Entity<Client>()
.Property(ap => ap.ActivationStatus)
.HasDefaultValue(1);
modelBuilder.Entity<Revendeur>()
.Property(ap => ap.ActivationStatus)
.HasDefaultValue(1);
modelBuilder.Entity<WorkerLocal>()
.Property(ap => ap.ActivationStatus)
.HasDefaultValue(1);
modelBuilder.Entity<ConfigurationWorker>()
.Property(cw => cw.Jpeg2000LossyRate)
.HasDefaultValue(40);
modelBuilder.Entity<ConfigurationWorker>()
.Property(cw => cw.ActiverBackgroundServiceWorker)
.HasDefaultValue(true);
modelBuilder.Entity<WorkerLocal>()
.Property(ap => ap.EnLigne)
.HasDefaultValue(false);
modelBuilder.Entity<WorkerLocal>()
.Property(ap => ap.UtilisationCPUPourcentage)
.HasDefaultValue(0.00);
modelBuilder.Entity<WorkerLocal>()
.Property(ap => ap.UtilisationRAMPourcentage)
.HasDefaultValue(0.00);
modelBuilder.Entity<WorkerLocal>()
.Property(ap => ap.UtilisationStockagePourcentage)
.HasDefaultValue(0.00);
modelBuilder.Entity<MappingHL7>()
.Property(m => m.SegmentId)
.HasDefaultValue(1);
modelBuilder.Entity<MappingHL7>()
.Property(m => m.ChampsHL7RepetitionId)
.HasDefaultValue(1);
modelBuilder.Entity<MappingHL7>()
.Property(m => m.SousComposantHL7)
.HasDefaultValue(1);
modelBuilder.Entity<MappingHL7>()
.Property(m => m.TypeMessage)
.HasDefaultValue("Tous");
}
public DbSet<RegleTraitementImage> ReglesTraitementImage { get; set; }
public DbSet<CritereSelectionRegle> CriteresSelectionRegle { get; set; }
public DbSet<AETDemandeur> AETDemandeurs { get; set; }
public DbSet<DestinatairePartageDocument> DestinatairesPartageDocuments { get; set; }
public DbSet<WPFFetcher> WPFFetchers { get; set; }
public DbSet<ClientWPFFetcher> ClientsWPFFetchers { get; set; }
public DbSet<ExamenAEnvoyer> ExamensAEnvoyer { get; set; }
public DbSet<ImageAEnvoyer> ImagesAEnvoyer { get; set; }
public DbSet<WorkerLocal> WorkersLocal { get; set; }
public DbSet<DestinationHL7> DestinationsHL7 { get; set; }
public DbSet<MessageHL7> MessagesHL7 { get; set; }
public DbSet<Patient> Patients { get; set; }
public DbSet<Examen> Examens { get; set; }
public DbSet<Medecin> Medecins { get; set; }
public DbSet<Correspondant> Correspondants { get; set; }
public DbSet<ExamensCorrespondants> ExamensCorrespondants { get; set; }
public DbSet<MappingHL7> MappingHL7 { get; set; }
public DbSet<ConfigurationWorker> ConfigurationWorker { get; set; }
public DbSet<DocumentStatus> DocumentStatus { get; set; }
public DbSet<Document> Documents { get; set; }
public DbSet<DocumentLogEvents> DocumentLogEvents { get; set; }
public DbSet<DestinatairePartage> DestinatairesPartage { get; set; }
public DbSet<Client> Clients { get; set; }
public DbSet<Revendeur> Revendeurs { get; set; }
public DbSet<ClientWorkerLocal> ClientWorkersLocal { get; set; }
public DbSet<DataProtectionKey> DataProtectionKeys { get; set; }
public DbSet<MappingExamenCompression> MappingExamenCompression { get; set; }
}
Has anyone any idea how to prevent this error from happening ?
I have tried changing the onDelete constraint but it didn't change anything.
Thanks for your help !

After clearing the table everything works fine.
I think I had foreign key id from my previous tests that didn't exist anymore in the original table.
Sorry to bother you !

Related

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Posts_Authors_AuthorId"

I successfully built migrations, and I am now trying to update the database with my models in Asp.net core but it keeps giving me this error
"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Posts_Authors_AuthorId". The conflict occurred in database "MediumDb", table "dbo.Authors", column 'AuthorId'.
The statement has been terminated."
This is what the code in my Post class looks like
namespace Medium.Api.Entities
{
public class Post
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int NoOfClaps { get; set; }
public DateTime CreatedDate { get; set; }
public IQueryable<Tag> Tags { get; set; }
public IQueryable<PostTag> PostTags { get; set; }
public string Image { get; set; }
// public string Video { get; set; }
public Author Author { get; set; }
public int AuthorId { get; set; }
while the code in my Author class says this
namespace Medium.Api.Entities
{
public class Author
{
public int AuthorId { get; set; }
public string Name { get; set; }
public IQueryable<Post> Posts { get; set; }
}
}
This is my DbContext configuration
{
public class MediumApiContext : DbContext
{
public MediumApiContext(DbContextOptions options)
: base(options)
{
// Database.EnsureCreated();
}
public DbSet<Post> Posts { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<PostTag> PostTags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Author>()
.HasKey(a => a.AuthorId);
modelBuilder.Entity<Author>()
.HasMany(a => a.Posts)
.WithOne(p => p.Author);
modelBuilder.Entity<Post>()
.ToTable("Posts");
modelBuilder.Entity<Post>()
.HasKey(p => p.Id);
modelBuilder.Entity<Post>()
.HasOne(p => p.Author)
.WithMany(a => a.Posts);
modelBuilder.Entity<Post>()
.Property(p => p.CreatedDate)
.IsRequired()
.HasColumnType("Date")
.HasDefaultValueSql("getutcdate()");
modelBuilder.Entity<Post>()
.Property(p => p.Title)
.IsRequired();
modelBuilder.Entity<Post>()
.Property(p => p.NoOfClaps)
.IsRequired();
modelBuilder.Entity<Post>()
.Property(p => p.Content)
.IsRequired();
I don't know where I seem to be getting it all wrong. Please
We Use FK for data integrity right now you have FOREIGN KEY with Author Table so :
"The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Posts_Authors_AuthorId". The conflict occurred in database "MediumDb", table "dbo.Authors", column 'AuthorId'. The statement has been terminated."
This means that when you create a Post, you must give an Author_ID that is on the Author table

Error when creating the table : Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints

I get this error:
Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
What is the problem in my code?
public class AppUserMap : IEntityTypeConfiguration<AppUser>
{
public void Configure(EntityTypeBuilder<AppUser> builder)
{
builder.Property(m => m.Name).HasMaxLength(50).IsRequired(true);
builder.HasMany(m => m.Essays).WithOne(m => m.AppUser).HasForeignKey(m => m.AppUserId);
}
}
public class AppRoleMap : IEntityTypeConfiguration<AppRole>
{
public void Configure(EntityTypeBuilder<AppRole> builder)
{
builder.HasKey(m => m.Id);
builder.HasMany(m => m.AppUsers).WithOne(m => m.AppRole).HasForeignKey(m => m.AppRoleId).OnDelete(DeleteBehavior.NoAction);
}
}
public class AppRole : IdentityRole<int>, ITable
{
public List<AppUser> AppUsers { get; set; }
}
public class AppUser : IdentityUser<int>, ITable
{
public string Name { get; set; }
public string Picture { get; set; } = "default.png";
#nullable enable
public string? AppUserRole { get; set; }
#nullable disable
public bool Ban { get; set; } = false;
public List<Essay> Essays { get; set; }
public AppRole AppRole { get; set; }
public int AppRoleId { get; set; }
}
Thank you for answering. I solved the issue by changing a couple of things in my code.
This is my solution code(you can see the changes):
public class AppUser : IdentityUser<int>, ITable
{
public string Name { get; set; }
public string Picture { get; set; } = "default.png";
public bool Ban { get; set; } = false;
public List<Essay> Essays { get; set; }
#nullable enable
public AppRole? AppRole { get; set; }
public int? AppRoleId { get; set; }
#nullable disable
}
public class AppRoleMap : IEntityTypeConfiguration<AppRole>
{
public void Configure(EntityTypeBuilder<AppRole> builder)
{
builder.HasKey(m => m.Id);
builder.HasMany(m => m.AppUsers).WithOne(m => m.AppRole).HasForeignKey(m => m.AppRoleId).OnDelete(DeleteBehavior.SetNull);
}
}

Multiplicity is not valid in Role Code First

Books.cs
public class Books
{
public int BookId { get; set; }
public int AuthorId { get; set; }
public int CategoryId { get; set; }
public int PublishinHouseId { get; set; }
public string IsbnNo { get; set; }
public string BookName { get; set; }
public int NumberOfPages { get; set; }
public string Location { get; set; }
public virtual Authors Author { get; set; }
public virtual Categories Category { get; set; }
public virtual PublishingHouses PublishingHouse { get; set; }
public virtual ICollection<Borrowers> Borrowers { get; set; }
}
Students.cs
public class Students
{
public int StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int GenderId { get; set; }
public int DepartmentId { get; set; }
public DateTime DateOfBirth { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public virtual Genders Gender { get; set; }
public virtual Departments Department { get; set; }
public virtual ICollection<Borrowers> Borrowers { get; set; }
}
Borrowers.cs
public class Borrowers
{
public int BorrowerId { get; set; }
public int BookId { get; set; }
public int StudentId { get; set; }
public DateTime DateGiven { get; set; }
public DateTime DateReceived { get; set; }
public virtual Books Book { get; set; }
public virtual Students Student { get;set ;}
}
BorrowerMap.cs
public BorrowerMap()
{
//Primary Key
this.HasKey(t => t.BorrowerId);
//Property
this.Property(t => t.BorrowerId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.DateGiven).IsRequired().HasColumnType("Date");
this.Property(t => t.DateReceived).IsRequired().HasColumnType("Date");
this.ToTable("Borrowers");
this.Property(t => t.BorrowerId).HasColumnName("BorrowerId");
this.Property(t => t.BookId).HasColumnName("BookId");
this.Property(t => t.StudentId).HasColumnName("StudentId");
this.Property(t => t.DateGiven).HasColumnName("DateGiven");
this.Property(t => t.DateReceived).HasColumnName("DateReceived");
this.HasRequired(p => p.Book).WithMany(p => p.Borrowers).HasForeignKey(f => f.BorrowerId);
this.HasRequired(p => p.Student).WithMany(p => p.Borrowers).HasForeignKey(f => f.StudentId);
}
Next>
Package Manager Console
PM> Add-Migration
cmdlet Add-Migration at command pipeline position 1
Supply values for the following parameters:
Name: MvcDbContext
ERROR:
One or more validation errors were detected during model generation:
Borrowers_Book_Source: : Multiplicity is not valid in Role
'Borrowers_Book_Source' in relationship 'Borrowers_Book'. Because the
Dependent Role refers to the key properties, the upper bound of the
multiplicity of the Dependent Role must be '1'.
Please help me?
The problem is from Visual Studio.
Good luck with.

ConnectionStrings DbContext in EF Core

I don't know what I am doing wrong, trying very hard to setup a database project, and keep getting errors related to the connectionstring dbcontext.
I have one applicationdbcontext which is just connecting to localdb
the problem is my other dbcontext which is where my data is seems to be in the wrong place and I am not sure how to fix it. This code is in my models folder
public DbSet<Customer> Customers { get; set; }
public DbSet<Job> Jobs { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Staff> Staff { get; set; }
public DbSet<RequestType> RequestType { get; set; }
public DbSet<CustomerJob> CustomerJobs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Customers;Trusted_Connection=True;");
}
in startup.cs I have this code..
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString("ProdConnection")));
in my appsettings config I have this code..
"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=BRSCRM;Trusted_Connection=True;MultipleActiveResultSets=true",
"ProdConnection": "Server=(localdb\\mssqllocaldb;Database=Customers;Trusted_Connection=True;MultipleActiveResults=true"
},
yet when I run my project I get an error that the configuration being supplied is not being used,DI inversion of control indeed, its a bag of cats and its on fire!
You should remove the optionsBuilder.UseSqlServer("... line in your OnConfiguring method.
Then add a constructor to your DbContext class like this;
public YourDbContext(DbContextOptions<YourDbContext> options) : base(options)
{
}
Adding DbContext class..
public class CustomerContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Job> Jobs { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Staff> Staff { get; set; }
public DbSet<RequestType> RequestType { get; set; }
public DbSet<CustomerJob> CustomerJobs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=Customers;Trusted_Connection=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//modelBuilder.Entity<CustomerJob>()
// .HasKey(c => new { c.JobId, c.CustomerId });
//code to require a staff member be assigned..
// modelBuilder.Entity<Staff>().Property(s => s.Name).IsRequired();
// modelBuilder.Entity<Customer>().Property(c => c.AssignedStaff).IsRequired();
}
}
public class CustomerJob
{
public int CustomerJobId { get; set; }
public int CustomerId { get; set; }
public DateTime RequestDate { get; set; }
public int JobId { get; set; }
public Job Job { get; set; }
}
public class Job
{
public int JobId { get; set; }
public int CustomerId { get; set; }
public string BusinessName { get; set; }
public string Name { get; set; }
public string JobDescription { get; set; }
public string ServiceType { get; set; }
public string GoogleLink { get; set; }
public string PoisLink { get; set; }
public bool EquisRendered { get; set; }
public bool NadirsRemoved { get; set; }
public string FolderLink { get; set; }
public string ReviewPosted { get; set; }
public string Ingestion { get; set; }
public string Moderated { get; set; }
public bool Delivered { get; set; }
public string CustomerReview { get; set; }
public string PublishedLink { get; set; }
public DateTime RequestDate { get; set; }
public DateTime LastModifiedDate { get; set; }
public DateTime ScheduleShootDate { get; set; }
public DateTime CompletionDate { get; set; }
public List<CustomerJob> CustomerJobs { get; set; }
public Staff AssignedStaff { get; set; }
}
public class Staff
{
public int StaffId { get; set; }
public string Name { get; set; }
public string Phone { get; set; }
public string EMail { get; set; }
}
public class Order
{
public int OrderID { get; set; }
public int CustomerID { get; set; }
public int Order_Detail_Id { get; set; }
public List<Job> Job { get; set; }
}
public class RequestType
{
public int ID { get; set; }
public string Description { get; set; }
}
}
Startup.cs class..
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<CustomerContext>();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
// Add Oauth Options
/* Third Party Login Authenticaton Options Google */
services.AddAuthentication().AddGoogle(googleOptions =>
{
googleOptions.ClientId = Configuration["Authentication:Google:ClientId"];
googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});
/* End Google Options */
/* Begin Facebook Options */
services.AddAuthentication().AddFacebook(facebookOptions =>
{
facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
});
/* End Facebook Options */
/* Begin Microsoft Options */
services.AddAuthentication().AddMicrosoftAccount(microsoftOptions =>
{
microsoftOptions.ClientId = Configuration["Authentication:Microsoft:ApplicationId"];
microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:Password"];
});
/* End Microsoft Options */
/* Twitter Options */
services.AddAuthentication().AddTwitter(twitterOptions =>
{
twitterOptions.ConsumerKey = Configuration["Authentication:Twitter:ConsumerKey"];
twitterOptions.ConsumerSecret = Configuration["Authentication:Twitter:ConsumerSecret"];
});
/* End Twitter Options */
/* Begin Identity Options Configuration */
services.AddMvc();
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdminRole", policy => policy.RequireRole("Admin"));
});
}
Anyways its working now, my mistake was that I had defined the connection in 2 seperate places and I guess that threw an exception.

Entity Framework 6 - Property name 'varchar' is already defined

I'am using EF6 and code firt in project, but i get this error on creating the model
Name: Each property name in a type must be unique. Property name 'varchar' is already defined.
below is my entity and my map
public class Menu
{
public Menu()
{
ListaFilhos = new List<Menu>();
}
public Int32 MenuID { get; set; }
public String Nome { get; set; }
public String Action { get; set; }
public String Controller { get; set; }
public String Url { get; set; }
public Int32? Pai { get; set; }
public Boolean? Ativo { get; set; }
public virtual List<Menu> ListaFilhos { get; set; }
}
Map
public class MenuMap : EntityTypeConfiguration<Entidade.Menu>
{
public MenuMap()
{
HasKey(x => x.MenuID);
Property(x => x.MenuID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(x => x.Pai).IsRequired();
Property(x => x.Nome).HasColumnType("varchar").HasMaxLength(100).IsRequired();
Property(x => x.Url).HasColumnName("varchar").HasMaxLength(250);
Property(x => x.Action).HasColumnName("varchar").HasMaxLength(50);
Property(x => x.Controller).HasColumnName("varchar").HasMaxLength(50);
Property(x => x.Ativo).IsRequired();
ToTable("Menu");
}
}
public class MenuGrupo
{
public MenuGrupo()
{
ListaMenu = new List<Menu>();
}
public Int32 MenuGrupoID { get; set; }
public Int32 MenuID { get; set; }
public virtual List<Menu> ListaMenu { get; set; }
}
public class MenuGrupoMap : EntityTypeConfiguration<Entidade.MenuGrupo>
{
public MenuGrupoMap()
{
HasKey(x => x.MenuGrupoID);
Property(x => x.MenuGrupoID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(x => x.MenuID).IsRequired();
ToTable("MenuGrupo");
}
}
I guess my problem is on MenuMap, when i remove in the OnModelCreate, all orther map working.
What i'am doing wrong?
tks

Resources