Run*_*sen 4 c# entity-framework-core
我的情况下,我想存储一个地址,但它必须是可选的。
我的映射如下所示:
map.OwnsOne(x => x.Address, cb => cb.OwnsOne(l => l.Location));
Run Code Online (Sandbox Code Playgroud)
但是当将我的DbContext的地址设置为空iam时出现此错误:
InvalidOperationException:'Member'的实体正在与'Member.Address#StreetAddress'共享表'Members',但是没有具有相同键值'Id:-2147480644'的此类型的实体被标记为'Added '。
然后,我从构造函数实例化了Address和Location,现在可以保存实体。但是,当再次获取数据时,我也得到了一个实例化的地址,我真的想要一个空值。
不能创建可为空的拥有类型吗?
实体框架文档指出
对拥有的实体类型的引用导航不能为 null,除非它们显式映射到与所有者不同的单独表
所以,事实上,你的问题有一个解决方案。您需要将您拥有的实体映射到单独的表,而不是将其与所有者放在同一个表中。
map.OwnsOne(x => x.Address, cb => cb.OwnsOne(l => l.Location, l=> l.ToTable("Locations")));
Run Code Online (Sandbox Code Playgroud)
通过将位置实体映射到名为Locations的单独表中,拥有的实体可以为空。
拥有类型的限制之一是不支持可选(即可为空)。我建议你关注这个线程。
在我的解决方案中,我使用 Empty Object 方法并使用 IsEmpty 方法来了解地址是否为空,而不是询问地址是否为空。我希望这种方法可以帮助你。
public sealed class Address : ValueObject<Address>
{
public string StreetAddress1 { get; private set; }
public string StreetAddress2 { get; private set; }
public string City { get; private set; }
public string State { get; private set; }
public string ZipCode { get; private set; }
public string Country { get; private set; }
private Address() { }
public Address(string streetAddress1, string city, string state, string zipcode, string country)
{
StreetAddress1 = streetAddress1;
City = city;
State = state;
ZipCode = zipcode;
Country = country;
}
public Address(string streetAddress1, string streetAddress2, string city, string state, string zipcode, string country)
: this(streetAddress1, city, state, zipcode, country)
{
StreetAddress2 = streetAddress2;
}
public static Address Empty()
{
return new Address("", "", "", "", "");
}
public bool IsEmpty()
{
if (string.IsNullOrEmpty(StreetAddress1)
&& string.IsNullOrEmpty(City)
&& string.IsNullOrEmpty(State)
&& string.IsNullOrEmpty(ZipCode)
&& string.IsNullOrEmpty(Country))
{
return true;
}
else
{
return false;
}
}
}
public class Firm : AggregateRoot<Guid>
{
public string Name { get; private set; }
public Address Address { get; private set; }
private Firm() { }
public Firm(string name)
{
if (String.IsNullOrEmpty(name))
throw new ArgumentException();
Id = Guid.NewGuid();
Name = name;
Address = Address.Empty();
}
}
Run Code Online (Sandbox Code Playgroud)
不能创建可为空的拥有类型吗?
从EF Core 3开始,这已经成为可能。
现在所有依赖项都是可选的。(在预览版4中发货): 来源
样例代码:
static void Main(string[] args)
{
using (var context = new OwnedEntityContext())
{
context.Add(new DetailedOrder
{
Status = OrderStatus.Pending,
OrderDetails = new OrderDetails
{
ShippingAddress = new StreetAddress
{
City = "London",
Street = "221 B Baker St"
}
//testing 3.0: "Yes, all dependents are now optional"
//reference: https://github.com/aspnet/EntityFrameworkCore/issues/9005#issuecomment-477741082
//NULL Owned Type Testing
//BillingAddress = new StreetAddress
//{
// City = "New York",
// Street = "11 Wall Street"
//}
}
});
context.SaveChanges();
}
//read test
using (var context = new OwnedEntityContext())
{
#region DetailedOrderQuery
var order = context.DetailedOrders.First(o => o.Status == OrderStatus.Pending);
Console.Write("NULL Owned Type Test, Is Billing Address NULL?");
//PRINTS FALSE
Console.WriteLine($"{order.OrderDetails.BillingAddress == null}");
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
#region OwnsOneNested
modelBuilder.Entity<DetailedOrder>().OwnsOne(p => p.OrderDetails, od =>
{
od.OwnsOne(c => c.BillingAddress);
od.OwnsOne(c => c.ShippingAddress);
});
#endregion
#region OwnsOneTable
modelBuilder.Entity<DetailedOrder>().OwnsOne(p => p.OrderDetails, od =>
{
od.OwnsOne(c => c.BillingAddress);
od.OwnsOne(c => c.ShippingAddress);
od.ToTable("OrderDetails");
//Exception message:Microsoft.Data.SqlClient.SqlException:
//'Cascading foreign key 'FK_OrderDetails_DetailedOrders_OrderId' cannot
//be created where the referencing column 'OrderDetails.OrderId' is an identity column.
//Could not create constraint or index. See previous errors.'
//3.0 bug: https://github.com/aspnet/EntityFrameworkCore/issues/17448#issuecomment-525444101
//fixed in 3.1: https://github.com/aspnet/EntityFrameworkCore/pull/17458
od.Property("OrderId")
.ValueGeneratedNever();
});
#endregion
}
Run Code Online (Sandbox Code Playgroud)