ef code first - Asp.Net MVC Codefirst Model Relation to the Same Model -


I have a model and I want to put the same model to an additional area which can be filled with the form For Ireland:. Categories and sub categories

In my example, visitors can add a file type but the file type can be selected if another file type is being done, but I can not work on it. Below you can see my model Public class HR file type {{Key} Public Ent ID {Receive; Set; } [Display (name = "Dojo Edu")] Public at name {get; Set; } Public int? HrFileTypeId {Receive; Set; } Public Virtual HrFileType HrFileType2 {get; Set; }}

You just need to add a to foreignKeyAttribute As follows:

  public class HrFileType {[Key] public int id {get; Set; } [Display (name = "Dojo Edu")] Public at name {get; Set; } Public int? HrFileTypeId {Receive; Set; } [Foreign key ("HR filetype ID")] Public virtual HR file type HR file type 2 {Received; Set; }}   

You can also use the fluent API to achieve this goal:

  public class HrFileType {[Key] Public Integer ID {Find; Set; } [Display (name = "Dojo Edu")] Public at name {get; Set; } Public int? HrFileTypeId {Receive; Set; } Public Virtual HrFileType HrFileType2 {get; Set; }} Public category YourDbContext: DbContext {public DbSet & lt; HrFileType & gt; HrFileTypes {get; Set; } Protected Override Zero OnModelCreating (DbModelBuilder modelBuilder) {// modelBuilder.Entity & LT; HrFileType & gt; () .HasOptional (c = & gt; c.HrFileType2) .WithMany () .HasForeignKey (c = & gt; c.HrFileTypeId); }}    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -