Closed holyrong closed 4 years ago
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(
SchoolId
andUserId
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.
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; }
}
@github-ingenium Thanks for answer, mine came with this error:
so I have to used the below:
[NotMapped]
public override ExtraPropertyDictionary ExtraProperties { get; protected set; }
@github-ingenium Thanks for answer, mine came with this error:
so I have to used the below:
[NotMapped] public override ExtraPropertyDictionary ExtraProperties { get; protected set; }
This doesn't work for me because at the next Migration, a new table of AppUser is created.
Don't think this should be closed, there still no way to use a Navigation property of AppUser in another entity, without it, EF won't create the FK
@CAIOHOBORGHI
This is the solution I'm using which is working:
YourProject.Domain/SomeEntities/SomeEntity.cs
public class SomeEntity: AuditedAggregateRoot<Guid>
{
// OTHER PROPERTIES
public Guid UserId { get; set; }
}
YourProject.EntityFrameworkCore/EntityFrameworkCore/YourProjectDbContextModelCreatingExtensions.cs
builder.Entity<SomeEntity>(b =>
{
b.ToTable("SomeEntity", YourProjectConsts.DbSchema);
b.ConfigureByConvention(); //auto configure for the base class props
b.HasOne<IdentityUser>().WithMany().HasForeignKey(x => x.UserId);
});
YourProject.EntityFrameworkCore/EntityFrameworkCore/YourProjectDbContext.cs
[ConnectionStringName("Default")]
public class YourProjectDbContext : AbpDbContext<YourProjectDbContext>
{
public DbSet<AppUser> Users { get; set; }
public DbSet<SomeEntity> SomeEntities { get; set; }
/* Add DbSet properties for your Aggregate Roots / Entities here.
* Also map them inside YourProjectDbContextModelCreatingExtensions.ConfigureYourProject
*/
public YourProjectDbContext(DbContextOptions<YourProjectDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
/* Configure the shared tables (with included modules) here */
builder.Entity<AppUser>(b =>
{
b.ToTable(AbpIdentityDbProperties.DbTablePrefix + "Users"); //Sharing the same table "AbpUsers" with the IdentityUser
b.ConfigureByConvention();
b.ConfigureAbpUser();
b.ConfigureExtraProperties();
/* Configure mappings for your additional properties
* Also see the YourProjectEfCoreEntityExtensionMappings class
*/
});
// BUG: Below is a hack to overcome IdentityUser issues
builder.Entity<IdentityUser>().Ignore(x => x.ExtraProperties);
builder.Entity<IdentityUserLogin>().HasKey(x => new { x.UserId, x.LoginProvider });
builder.Entity<IdentityUserRole>().HasKey(x => new { x.UserId, x.RoleId });
builder.Entity<IdentityUserToken>().HasKey(x => new { x.UserId, x.LoginProvider });
builder.Entity<IdentityUserOrganizationUnit>().HasKey(x => new { x.UserId, x.OrganizationUnitId });
/* Configure your own tables/entities inside the ConfigureYourProject method */
builder.ConfigureYourProject();
}
}
YourProject.Domain/Users/AppUser.cs
[NotMapped]
public override ExtraPropertyDictionary ExtraProperties { get; protected set; }
Thanks @Znow , with this the FK is created.
Still can't use the navigation property but it's better than no FK at all.
I haven't tested this yet but I have used a solution based on both @Znow and @yekalkan answers
if (isMigrationDbContext)
{
t.Ignore(x => x.AppUser); // 1st point
t.HasOne<IdentityUser>().WithMany().HasForeignKey(x => x.AppUserId); // 2nd point
}
else
t.HasOne(x => x.AppUser).WithMany().HasForeignKey(x => x.AppUserId); // 3rd point
Can't use AbpUser as navigation property
error AppUser
tablecheck @yekalkan answer to get the full picture
I have the same issue here while my AppUser still my aggregate root, I don't even put AppUser as Navigation property inside my entity.
builder.Entity<UserSession>(u =>
{
u.ToTable(
ProtocloudConsts.DbTablePrefix + "UserSession",
ProtocloudConsts.DbSchema);
u.HasOne<AppUser>().WithOne().HasForeignKey<AppUser>(x => x.Id);
u.ConfigureByConvention();
u.Property(x => x.AutodeskUserID).IsRequired();
u.Property(x => x.UserName).IsRequired();
u.Property(x => x.MacID).IsRequired();
});
Do we have an official solution for this? Or I have to do a workaround?
I have the same issue without any solution yet.
I haven't tested this yet but I have used a solution based on both @Znow and @yekalkan answers
if (isMigrationDbContext) { t.Ignore(x => x.AppUser); // 1st point t.HasOne<IdentityUser>().WithMany().HasForeignKey(x => x.AppUserId); // 2nd point } else t.HasOne(x => x.AppUser).WithMany().HasForeignKey(x => x.AppUserId); // 3rd point
- avoids the
Can't use AbpUser as navigation property
error- creates the foreign key relation (without creating the
AppUser
table- should solve the navigation property issue (not tested yet)
check @yekalkan answer to get the full picture
I already do this and navigation property display: Invalid object name 'AppUser' Then not found for me.
I don’t use navigation properties, just want to set a foreign key in database. I have the same issue, is there a solution?
Is there any official solution for the issue yet?
hello
try this after entity configuration
builder.ConfigureByConvention();
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:
Config:
when add-migration, I've received a error:
if replaced
with
b.Ignore(t => t.User);
add-migration succeed, but when I executeRepository.WithDetails()
in SchoolAppService, I received another error:This only occured when Relation with AbpUser 。
I've tried #1414 ,It's not working.
@hikalkan , @maliming ,can you help me?