使用 Fluent API 和 ObjectContext 生成数据注释

发布于 2024-10-30 16:13:55 字数 629 浏览 6 评论 0原文

我正在使用 MVC 3 和 Entity Framework 4 构建一个应用程序。 我已经创建了实体数据模型并从中生成了一个数据库。 现在我知道可以在模型属性上使用 [Required] 或 [StringLength(5)] 等验证属性来提供客户端和服务器端的验证。

我想知道这些属性是否也可以动态生成,而不必将它们显式添加到模型中?我发现在 EF 4.1 RC 中,您可以使用 Fluent API 通过使用 DbModelBuilder 类在 OnModelCreating 方法中进一步配置模型。 如图所示 这里 我正在使用一个框架,但它仍然使用 ObjectContext 而不是 DbContext,所以我想知道上述解决方案是否可以与 ObjectContext 结合使用?

最后一点,由于我一直在尝试弄清楚如何生成和使用数据注释,因此使用视图模型似乎会增加验证的复杂性。从我在这里读到的内容似乎只需将模型直接传递给视图就无需向模型以及视图模型添加注释。然而,这意味着当您在模型上进行连接并将其直接传递给视图时,您不能再使用强类型视图?

I'm building an application using MVC 3 and Entity Framework 4.
I've created my Entity Data Model and generated a database from it.
Now I know the validation attributes such as [Required] or [StringLength(5)] can be used on the model properties to provide validation both clientside and serverside.

I would like to know if these attributes can also be generated dynamically instead of having to add them to the model explicitly? I saw that in EF 4.1 RC you can make use of the Fluent API to further configure your model in the OnModelCreating method by using the DbModelBuilder class.
As shown here
I'm working with a framework however that still uses ObjectContext instead of DbContext so I would like to know if the above solution can be used in combination with ObjectContext?

As a final note, since I've been trying to figure out how to generate and use data annotations it seems using view models would increase the complexity of validation. From what I read here it seems that just passing the models directly to the view would remove the need to add annotations to the models as well as the view models. However that means that you can no longer use strongly typed views when you do joins on the models and pass those to the view directly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小霸王臭丫头 2024-11-06 16:13:55

不,不能。 Fluent API 是描述映射的不同方法。您可以使用 Fluent API 或 EDMX(实体数据模型)。两者都不是。 Fluent API 也仅适用于 DbContext API。如果您想生成注释,您可以尝试修改生成类的 T4 模板。

No it can't. Fluent API is different approach to describe mapping. You can use fluent API or EDMX (Entity Data Model). Not both. Fluent API also works only with DbContext API. If you want to have annotations generated you can try to modify T4 template generating your classes.

小鸟爱天空丶 2024-11-06 16:13:55

在使用扩展基类的 poco 类时,我遇到了一个令人不安的问题。
例如,假设您有一个具有强类型 Car 属性的 Person poco 类。您还有一个配偶 poco,它也使用汽车财产。

现在您想要使用 Display("Name = xxx") 属性在视图中显示“个人汽车”和“配偶汽车”。你不能!如果您不使用平面视图模型,请注意此问题

I have come across a disturbing issue when using poco classes that are extending base classes.
For example, let say you have a Person poco class that has a strongly typed Car property. You also have a Spouse poco that also uses the Car Property.

Now you want to display "Person Car" and "Spouses Car" in the view using the Display("Name = xxx") attribute. You cant!!! Becareful of this issue if you are not using flat View Models

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文