I'm working with 0.18.1.I get the error message when add-migration.
The relation of entities like this:

The Entity "SchoolUsers" inherits from Entity like this:
public class SchoolUsers : Entity
{
public Guid SchoolId { get; set; }
public Guid UserId { get; set; }
public virtual Users.AppUser User { get; set; }
public virtual School School { get; set; }
public override object[] GetKeys()
{
return new object[2]
{
SchoolId,UserId
};
}
}
Config:
public override void Configure(ModelBuilder builder)
{
builder.Entity<Holyschool.Schools.SchoolUsers>(b =>
{
b.ToTable(HolyschoolConsts.DbTablePrefix + "SchoolUsers", HolyschoolConsts.DbSchema);
b.HasKey(cs => new { cs.SchoolId, cs.UserId });
b.HasOne(su => su.School)
.WithMany()
.HasForeignKey(su => su.SchoolId);
b.HasOne(su => su.User)
.WithMany()
.HasForeignKey(su => su.UserId);
// b.ConfigureFullAuditedAggregateRoot();
});
}
```
builder.Entity
{
b.ToTable("AbpUsers"); //Sharing the same table "AbpUsers" with the IdentityUser
//b.ConfigureFullAudited();
//b.ConfigureExtraProperties();
//b.ConfigureConcurrencyStamp();
b.ConfigureFullAuditedAggregateRoot();
b.ConfigureAbpUser();
//Moved customization to a method so we can share it with the HolyschoolMigrationsDbContext class
b.ConfigureCustomUserProperties();
});
when add-migration, I've received a error:
> The property 'AppUser.ExtraProperties' could not be mapped, because it is of type 'Dictionary<string, object>' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
if replaced
b.HasOne(su => su.User)
.WithMany()
.HasForeignKey(su => su.UserId);
```
with
b.Ignore(t => t.User);
add-migration succeed, but when I execute Repository.WithDetails() in SchoolAppService, I received another error:
[Error] The property 'User' is not a navigation property of entity type 'SchoolUsers'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names. System.InvalidOperationException: The property 'School' is not a navigation property of entity type 'SchoolUsers'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names.
This only occured when Relation with AbpUser 。
I've tried #1414 ,It's not working.
@hikalkan , @maliming ,can you help me?
try b.ConfigureExtraProperties();
try
b.ConfigureExtraProperties();
The SchoolUsers is inherit from Entity, there is not any b.ConfigureExtraProperties(); for object 'b'.
I've tried b.ConfigureFullAuditedAggregateRoot(); , the error still exists.
if i delete
b.HasOne(su => su.User)
.WithMany()
.HasForeignKey(su => su.UserId);
and add b.Ignore(t => t.User); the error disappeared.
Dont you have this code in your MyProjectDbContext.cs?
builder.Entity<AppUser>(b =>
{
b.ToTable("AbpUsers"); //Sharing the same table "AbpUsers" with the IdentityUser
b.ConfigureFullAuditedAggregateRoot();
b.ConfigureAbpUser();
//Moved customization to a method so we can share it with the MyProjectMigrationsDbContext class
b.ConfigureCustomUserProperties();
});
Dont you have this code in your MyProjectDbContext.cs?
builder.Entity(b =>
{
b.ToTable("AbpUsers"); //Sharing the same table "AbpUsers" with the IdentityUser
b.ConfigureFullAuditedAggregateRoot();
b.ConfigureAbpUser();//Moved customization to a method so we can share it with the MyProjectMigrationsDbContext class b.ConfigureCustomUserProperties(); });
This is my code:
builder.Entity<AppUser>(b =>
{
b.ToTable("AbpUsers"); //Sharing the same table "AbpUsers" with the IdentityUser
b.ConfigureFullAudited();
b.ConfigureExtraProperties();
b.ConfigureConcurrencyStamp();
b.ConfigureAbpUser();
//Moved customization to a method so we can share it with the HolyschoolMigrationsDbContext class
b.ConfigureCustomUserProperties();
});
This is a common scenario. While we don't suggest to have navigation properties to other aggregate roots in DDD, it is a common habit among EF Core developers :)
We will reproduce this and will see what we can suggest.
@yekalkan can you create a new application from the startup template, create the same entities and reproduce the problem.
try this:
if (isMigrationDbContext)
{
b.Ignore(t => t.User);
}
else
{
b.HasOne(su => su.User)
.WithMany()
.HasForeignKey(su => su.UserId);
}
Send isMigrationDbContext as paramater from MyProjectMigrationsDbContext and MyProjectDbContext.
in MyProjectMigrationsDbContext:

in MyProjectDbContext:

I wrote a question about this issue on stack overflow, any help is welcome, thanks!
Sorry I don't understand why this issue is closed, I think it has not been resolved.
@yekalkan
I tried your way, but I got only one foreign key ---- no user foreign key, but it's supposed to be two(SchoolId and UserId for the @holyrong 's sample).
Whenever an entity has relationship with AppUser(one to many, or many to many), this migration will fail with the "The property 'AppUser.ExtraProperties' could not be mapped" message.
Sorry I don't understand why this issue is closed, I think it has not been resolved.
@yekalkan
I tried your way, but I got only one foreign key ---- no user foreign key, but it's supposed to be two(SchoolIdandUserIdfor the @holyrong 's sample).Whenever an entity has relationship with
AppUser(one to many, or many to many), this migration will fail with the "The property 'AppUser.ExtraProperties' could not be mapped" message.
I agree with you, and this is a temporary solution I found, I hope it can help you in current situation.
https://stackoverflow.com/questions/49986756/efcore-map-2-entities-to-same-table
https://github.com/abpframework/abp/issues/1414#issuecomment-507395552
This is a common scenario. While we don't suggest to have navigation properties to other aggregate roots in DDD, it is a common habit among EF Core developers :)
We will reproduce this and will see what we can suggest.
is that meaning never ever use navigation properties in DDD in all conditions?
I was not using ExtraProperties so below solved my issue..
public class AppUser : FullAuditedAggregateRoot<Guid>, IUser
{
// Omitted for brevity
[NotMapped]
public override Dictionary<string, object> ExtraProperties { get; protected set; }
}
Most helpful comment
is that meaning never ever use navigation properties in DDD in all conditions?