如何在Entity Framework Core 2.0中为拥有的实体类型指定默认属性值?

编程入门 行业动态 更新时间:2024-10-09 04:25:28
本文介绍了如何在Entity Framework Core 2.0中为拥有的实体类型指定默认属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个简单的POCO类型,例如

I have a simple POCO type, say something like

public class OwnedEntity { public string stringProperty { get; set; } public decimal decimalProperty { get; set; } public bool boolProperty { get; set; } public int intProperty { get; set; } }

以及具有 OwnedEntity 参考

public class SomeEntity { public string Id { get; set; } public OwnedEntity OwnedEntity { get; set; } }

我按照文档使用EF Core的Fluent API:

I set up the relationship like described in the documentation using EF Core's Fluent API:

protected override void OnModelCreating (ModelBuilder builder) { base.OnModelCreating (builder); builder.Entity<SomeEntity> ().OwnsOne (e => e.OwnedEntity); }

关于如何为 OwnedEntity 。我试图像这样初始化属性:

I can't find anything on how to define default-values for all the properties of OwnedEntity. I tried to initialize the properties like this:

public class OwnedEntity { public string stringProperty { get; set; } = "initial" public decimal decimalProperty { get; set; } = -1M; public bool boolProperty { get; set; } = false; public int intProperty { get; set; } = -1; }

但无效。 [DefaultValueAttribute] 也是如此(但这是意料之中的,因为它是明确提及)。

but with no effect. Same goes with the [DefaultValueAttribute] (but that was to expect since it's explicitly mentioned).

有关如何处理常规实体的初始值的一些信息:

There's a bit of information on how to handle initial values for regular entities:

modelBuilder.Entity<SomeOtherEntity>() .Property(e => e.SomeIntProperty) .HasDefaultValue(3);

但是由于我遇到的是拥有实体类型,所以无法通过访问类型实体< T> 。

But since I'm facing an Owned Entity Type, I can't access the type via Entity<T>.

有没有一种方法可以满足我的需求?

Is there a way of doing what I'm looking for?

有些东西值得提到:

  • 我有很多特定实体,其中大多数都使用 OwnsOne 关系
  • 在基类中声明所有 OwnedEntity -属性不是一种选择,因为并非所有实体都具有这些属性
  • 我使用EF Core 2.0.3和ASP.NET Core MVC 2.0.4
  • I have a solid amount of specific entities where most of them are using the OwnsOne relation
  • Declaring all OwnedEntity-properties in a base class is not an option since not all the entities have those properties
  • I`m using EF Core 2.0.3 and ASP.NET Core MVC 2.0.4

最初,我想新创建一个 SomeEntity 实例,以便为所有嵌入式 SomeEntity.OwnedEntity预置属性。 属性。

Originally, I wanted to have newly created SomeEntity instances to come with preset properties for all of the 'embedded' SomeEntity.OwnedEntity properties.

但是看看我的关联控制器是如何工作的,这一切都说得通...我对创建操作有以下方法:

But looking at how my associated controller works, it all makes sense... I have the following methods for the 'Create' operation:

[HttpGet] public IActionResult Create () { return View (nameof (Create)); } [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Create (SomeEntity model) { context.Add (model); await context.SaveChangesAsync (); // redirect etc. }

这意味着未创建任何对象用于 Create 的 [HttGet] 重载以及链接到属性的所有HTML输入(通过 as-for )最初是空的。好的。所以我想这样做的正确方法是手动创建 SomeEntity 的新实例,并将其传递给 Create 像这样的视图:

Which means that no object is created for the [HttGet] overload of Create and all the HTML inputs linked to properties (via asp-for) are initially empty. Okay. So I guess the proper way of doing this is to manually create a new instance of SomeEntity and pass it to the Create view like this:

[HttpGet] public IActionResult Create () { return View (nameof (Create), new SomeEntity()); }

那么这是正确的方法还是要记住更多事情?

Is this the right approach then or are there some more things to keep in mind?

推荐答案

假设您了解EF Core是什么默认值适用于此,并且仅查找 Entity< T>()。Property(...)等价物。

Assuming you understand what EF Core Default Values are for, and just looking for equivalent of Entity<T>().Property(...) equivalent.

始终使用 ReferenceOwnershipBuilder< TEntity,TRelatedEntity> 类方法为每种所有者类型配置拥有的实体。要访问此类,请使用 OwnsOne 方法,或使用重载类型为 Action< ReferenceOwnershipBuilder< TEntity,TRelatedEntity>> 的第二个参数。

The owned entities are always configured for each owner type by using the ReferenceOwnershipBuilder<TEntity,TRelatedEntity> class methods. To access this class you either use the result of OwnsOne method, or use the OwnsOne overload taking second argument of type Action<ReferenceOwnershipBuilder<TEntity,TRelatedEntity>>.

例如,使用第二种方法:

For instance, using the second approach:

builder.Entity<SomeEntity>().OwnsOne(e => e.OwnedEntity, ob => { ob.Property(e => e.stringProperty) .HasDefaultValue("initial"); ob.Property(e => e.decimalProperty) .HasDefaultValue(-1M); // etc. });

更多推荐

如何在Entity Framework Core 2.0中为拥有的实体类型指定默认属性值?

本文发布于:2023-10-26 06:37:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中为   实体   属性   类型   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!