I would like to create tables in Entity Framework Core code first approach.
I have a table which is ApplicationUser. I have another table ShareDocument. In this ShareDocument table I have two property which are SenderUserId and ReceiverUserId. These two properties are the foreign key from ApplicationUser Table. Here is the entity framework code:
ApplicationUser Entity
public class ApplicationUser : IdentityUser
{
public string Id { get; set; }
public string Locale { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public IList<ShareDocument> SenderShareDocuments { get; set; }
public IList<ShareDocument> ReceiverShareDocuments { get; set; }
}
ShareDocument Entity
public class ShareDocument
{
public string Id { get; set; }
public string SenderUserId { get; set; }
public string ReceiverUserId { get; set; }
public ApplicationUser SenderApplicationUser { get; set; }
public ApplicationUser ReceiverApplicationUser { get; set; }
}
Here is the fluent api mapping class:
ApplicationUserMap
public class ApplicationUserMap : IEntityTypeConfiguration<ApplicationUser>
{
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
{
// Primary Key
builder.HasKey(x => x.Id);
// Properties
builder.Property(x => x.Id).HasMaxLength(450);
builder.Property(x => x.Locale).HasMaxLength(10).IsRequired();
// Relationships
builder.HasMany(x => x.SenderShareDocuments)
.WithOne(x => x.SenderApplicationUser)
.HasForeignKey(x => x.SenderUserId);
builder.HasMany(x => x.ReceiverShareDocuments)
.WithOne(x => x.ReceiverApplicationUser)
.HasForeignKey(x => x.ReceiverUserId);
}
}
ShareDocumentMap
public class ShareDocumentMap : IEntityTypeConfiguration<ShareDocument>
{
public void Configure(EntityTypeBuilder<ShareDocument> builder)
{
// Table & Column Mappings
builder.ToTable("ShareDocument");
// Primary Key
builder.HasKey(x => x.Id);
// Properties
builder.Property(x => x.Id).HasMaxLength(450);
builder.Property(x => x.SenderUserId).HasMaxLength(450);
builder.Property(x => x.ReceiverUserId).HasMaxLength(450);
// Relationships
builder.HasOne(x => x.SenderApplicationUser)
.WithMany(x => x.SenderShareDocuments)
.HasForeignKey(x => x.SenderUserId);
builder.HasOne(x => x.ReceiverApplicationUser)
.WithMany(x => x.SenderShareDocuments)
.HasForeignKey(x => x.ReceiverUserId);
}
}
And here is my DbContext class:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
{
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<ShareDocument> ShareDocuments { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.ApplyConfiguration(new ApplicationUserMap());
builder.ApplyConfiguration(new ShareDocumentMap());
}
}
Now when I runn the command Add-Migration I got the below errors.
Cannot create a relationship between 'ApplicationUser.SenderShareDocuments' and 'ShareDocument.ReceiverApplicationUser' because a relationship already exists between 'ApplicationUser.ReceiverShareDocuments' and 'ShareDocument.ReceiverApplicationUser'. Navigations can only participate in a single relationship. If you want to override an existing relationship call 'Ignore' on the navigation 'ShareDocument.ReceiverApplicationUser' first in 'OnModelCreating'.