I have the following (simplified) Classes:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFTest
{
public class TextContext : DbContext
{
public DbSet<People> People { get; set; }
public DbSet<File> Files { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
}
public class People
{
public People()
{
Files = new List<File>();
}
public int PeopleId { get; set; }
public string Nombre { get; set; }
public List<File> Files { get; set; }
}
public class File
{
public File()
{
Friends = new List<People>();
Foes = new List<People>();
}
public Int16 FileId { get; set; }
public List<People> Friends { get; set; }
public List<People> Foes { get; set; }
}
}
When the db is created I expect it to have a Joint table for people and files. But none was created, just the table People and the table Files.
The following is an image of the Entity Data Model
I Expected that the model to show a Many to Many relationship as each people may have many files and each file may have many people
I'm quite new with EF, I know I need to use the Fluent Api to configure the relation but so far all the code I have tried has failed.
Thanks for the help
Try setting your related entities with the virtual
keyword so that EF can override and implement those navigation properties itself. Also, I tend to use interfaces for the properties instead on concrete classes. Your call...
public class People
{
public People()
{
Files = new List<File>();
}
public int PeopleId { get; set; }
public string Nombre { get; set; }
public virtual ICollection<File> Files { get; set; }
}
public class File
{
public File()
{
Friends = new List<People>();
Foes = new List<People>();
}
public Int16 FileId { get; set; }
public virtual ICollection<People> Friends { get; set; }
public virtual ICollection<People> Foes { get; set; }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.