-
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
Conventions shouldn't try to set the base type for owned types #9536
Comments
@bubibubi Are you asking for inheritance of containment? If the former, can you add a bit more detail as to what the types look like and how you expect them to be mapped? If the latter, then you can already do: public class Foo
{
public int Id { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street { get; set; }
public City City { get; set; }
}
public class City
{
public string Name { get; set; }
public string State { get; set; }
}
modelBuilder.Entity<Foo>()
.OwnsOne(e => e.Address)
.OwnsOne(e => e.City); |
@ajcvickers I'd like to map something like this public class Friend
{
public Friend()
{
Address = new FullAddress();
}
public int Id { get; set; }
public string Name { get; set; }
public FullAddress Address { get; set; }
}
public class LessThanFriend
{
public LessThanFriend()
{
Address = new CityAddress();
}
public int Id { get; set; }
public string Name { get; set; }
public CityAddress Address { get; set; }
}
public class CityAddress
{
public string Cap { get; set; }
public string City { get; set; }
}
public class FullAddress : CityAddress
{
public string Street { get; set; }
}
public class TestContext : DbContext
{
public TestContext(DbContextOptions options) : base(options) { }
public DbSet<Friend> Friends { get; set; }
public DbSet<LessThanFriend> LessThanFriends { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// This Address is a full address. The problem is here
modelBuilder.Entity<Friend>()
.OwnsOne(_ => _.Address);
// This Address is a city address
modelBuilder.Entity<LessThanFriend>()
.OwnsOne(_ => _.Address);
}
} |
BaseTypeDiscoveryConvention shouldn't be running on OwnedTypes since they don't allow inheritance.
|
@bubibubi Couple of additional questions:
|
@ajcvickers in general I'm expecting that EF handles a hierarchy of classes as a single class. About first question, I'm not expecting that all the properties of the assigned object are persisted. About the second question I'm expecting that the columns of LessThanFriend are able to contain the properties of the class specified in OwnsOne (so the properties of CityAddress). In my example I called both the properties Address and is a little bit confusing. Here the tables I'm expecting. CREATE TABLE [Friends] (
[Id] int not null identity(1,1),
[Name] text null,
[Address_Street] text null,
[Address_Cap] text null,
[Address_City] text null,
);
CREATE TABLE [LessThanFriends] (
[Id] int not null identity(1,1),
[Name] text null,
[Address_Cap] text null,
[Address_City] text null
);
|
Triage decision: for now, we will throw a better exception. Issue #9630 filed for future to actual map these types. @bubibubi Thanks for the great feedback. We're not planning to support this now--see #9630--but you can workaround the issue by using only leaves for your owned types. For example: public class AddressBase
{
public string Cap { get; set; }
public string City { get; set; }
}
public class CityAddress : AddressBase
{
}
public class FullAddress : AddressBase
{
public string Street { get; set; }
} |
Fixed in 2507a80 |
For example an owned type Address { Street : string, City : City } and an owned type City { Name : string , State : string}
The text was updated successfully, but these errors were encountered: