admin管理员组文章数量:1393843
My domain:
public struct LatLng
{
public double Lat { get; set; }
public double Lng { get; set; }
}
public class Building
{
public LatLng? LatLng { get; set; }
}
I want LatLng
as an EF v8 complex type which is optional, but whose properties are required:
modelBuilder.Entity<Building>()
.ComplexProperty(x => x.LatLng, x => {
x.Property(y => y.Value.Latitude).IsRequired();
x.Property(y => y.Value.Longitude).IsRequired();
x.IsRequired(false);
});
The compiler warns "Nullable value type may be null. (CS8629)" which I can suppress (y!.Value
) as it doesn't seem to make a difference for the internal model built by EF.
But when adding a migration I get an error:
Unable to create a 'DbContext' of type ''. The exception 'The expression 'y => y.Value.Latitude' is not a valid member access expression. The expression should represent a simple property or field access: 't => t.MyProperty'. (Parameter 'memberAccessExpression')' was thrown while attempting to create an instance.
How does one do this?
(Note this is just an example of the problem. I could rely on conventions in this case, but in others I need to perform custom config. I'd like to know how to do it.)
My domain:
public struct LatLng
{
public double Lat { get; set; }
public double Lng { get; set; }
}
public class Building
{
public LatLng? LatLng { get; set; }
}
I want LatLng
as an EF v8 complex type which is optional, but whose properties are required:
modelBuilder.Entity<Building>()
.ComplexProperty(x => x.LatLng, x => {
x.Property(y => y.Value.Latitude).IsRequired();
x.Property(y => y.Value.Longitude).IsRequired();
x.IsRequired(false);
});
The compiler warns "Nullable value type may be null. (CS8629)" which I can suppress (y!.Value
) as it doesn't seem to make a difference for the internal model built by EF.
But when adding a migration I get an error:
Unable to create a 'DbContext' of type ''. The exception 'The expression 'y => y.Value.Latitude' is not a valid member access expression. The expression should represent a simple property or field access: 't => t.MyProperty'. (Parameter 'memberAccessExpression')' was thrown while attempting to create an instance.
How does one do this?
(Note this is just an example of the problem. I could rely on conventions in this case, but in others I need to perform custom config. I'd like to know how to do it.)
Share Improve this question asked Mar 27 at 14:09 lonixlonix 21.4k29 gold badges137 silver badges280 bronze badges1 Answer
Reset to default 1I fot that I modeled Coordinates
as a struct; I originally wanted it as a "value object". Now that I noticed, I changed it to a reference type, and that seems to satisfy EF.
public class LatLng {
and
x.Property(y => y!.Latitude).IsRequired();
x.Property(y => y!.Longitude).IsRequired();
I'm not sure if this is the best approach, but it works for me.
本文标签: netEF Core config for optional complex type with required propertiesStack Overflow
版权声明:本文标题:.net - EF Core config for optional complex type with required properties - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744082463a2587901.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论