-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migration ef core 2.2 to 3.1 with owned entities #19789
Comments
@michielpeeters The top-level protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BigCustomer>().OwnsOne(c => c.Info);
modelBuilder.Entity<SmallCustomer>().OwnsOne(c => c.Info);
modelBuilder.Entity<ProjectCustomer>().OwnsOne(c => c.Info);
modelBuilder.Entity<ContactInfo>()
.Property<byte[]>("RowVersion").IsRowVersion().HasColumnName("RowVersion");
base.OnModelCreating(modelBuilder);
} Do this: protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<BigCustomer>()
.OwnsOne(
c => c.Info,
b=> b.Property<byte[]>("RowVersion").IsRowVersion().HasColumnName("RowVersion"));
modelBuilder
.Entity<SmallCustomer>()
.OwnsOne(
c => c.Info,
b=> b.Property<byte[]>("RowVersion").IsRowVersion().HasColumnName("RowVersion"));
modelBuilder
.Entity<ProjectCustomer>()
.OwnsOne(
c => c.Info,
b=> b.Property<byte[]>("RowVersion").IsRowVersion().HasColumnName("RowVersion"));
base.OnModelCreating(modelBuilder);
} However, that's not working for me either. @AndriySvyryd Any ideas? Code: [Table("Customers")]
public abstract class CustomerBase
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
}
public class BigCustomer : CustomerBase
{
public ContactInfo Info { get; set; }
}
[Owned]
public class ContactInfo
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Email { get; set; }
}
public class ProjectCustomer : CustomerBase
{
public ContactInfo Info { get; set; }
}
public class SmallCustomer : CustomerBase
{
public ContactInfo Info { get; set; }
}
public class BloggingContext : DbContext
{
private readonly ILoggerFactory Logger
= LoggerFactory.Create(c => c.AddConsole());//.SetMinimumLevel(LogLevel.Debug));
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseLoggerFactory(Logger)
.EnableSensitiveDataLogging()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CustomerBase>();
modelBuilder
.Entity<BigCustomer>()
.OwnsOne(
c => c.Info,
b=> b.Property<byte[]>("RowVersion").IsRowVersion().HasColumnName("RowVersion"));
modelBuilder
.Entity<SmallCustomer>()
.OwnsOne(
c => c.Info,
b=> b.Property<byte[]>("RowVersion").IsRowVersion().HasColumnName("RowVersion"));
modelBuilder
.Entity<ProjectCustomer>()
.OwnsOne(
c => c.Info,
b=> b.Property<byte[]>("RowVersion").IsRowVersion().HasColumnName("RowVersion"));
base.OnModelCreating(modelBuilder);
}
}
public class Program
{
public static async Task Main()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
}
} Exception:
|
You also need to map the modelBuilder.Entity<CustomerBase>()
.Property(c => c.RowVersion).HasColumnName("RowVersion"); @ajcvickers Do you think that the exception message should be improved? |
@AndriySvyryd Maybe. I should have tried explicitly mapping it, but I was expecting to already map to that column. Why does it need explicit configuration here? Is it because RowVersion is explicitly mapped by other types, so we then uniquify the by-convention mapping for the column name? |
Yes. We uniquify all names not specified explicitly if there's a collision. |
Thank you for the responses. I was able to resolve my problem! Maybe it is an idea to mention that uniquify in the documentation? |
hello is this issue being resolved? i tried handled the owned entities and i am returning with the same error.
Code:
` Context Class: `
` Some handling for shadow properties are also being implemented. |
@clouddust0246 Don't call |
I had the same issue, and the fix was as following. the model I was working on is the following:
The configuration I used was:
The new configuration:
Add a migration and call it Sync to test. After some investigation, I found there were some changes to the file: ApplicationDbContextModelSnapshot The changes apparently not changing the model, but changing Model builder version to 3.1.4:
And some changes related to defining columns properties, for example:
I deleted the sync migration. Added it again and it was empty. Delete the second migration since it's empty. Run the application and it was intact. Hope this will help someone. |
Observed Behavior
I'm struggling with one of the breaking changes from ef core 2.2 to ef core 3.1. The breaking change that I'm talking about is the 'Owned' breaking change. I've tried many settings to resolve the problem that I'm facing, but until now, nothing has worked. My question is what must I do so that I can migrate from ef core 2.2 to ef core 3.1 succesfully (so that I'm able to use my context in the application without any data loss)?
I've managed to create a small test project where my problem is located which can be found here:
repository
I've tried and read the solutions from other people, but still no success:
#18092 #15681 #9873
https://stackoverflow.com/questions/57692369/ef-core-configuration-problem-with-owned-type-used-in-2-different-classes
https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities
The error that I'm experiencing is as follow:
The main question is, what can I do to resolve this error? :-)
Expected Behavior
That I'm able to run migrations and even better that I'm able to use the DbContext in the web project without this error.
Steps to reproduce
Github project: repository
Further technical details
EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 10
IDE: Visual Studio 2019 Preview 16.5.0 (Preview 2)
The text was updated successfully, but these errors were encountered: