FSharp记录类型与实体框架代码优先 [英] FSharp Record Types With Entity Framework Code-First

查看:80
本文介绍了FSharp记录类型与实体框架代码优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个业务应用程序中做一个概念证明,我想用一个F#代替现在的C#Code-First Entity Framework实现。我正在追踪这篇文章似乎工作得很好,但我希望使用FSharp记录类型,而不是文章使用的类。当我尝试添加数据注释到这样的记录类型时:

  type Family = {[< Key>] Id :INT;名字:串; IsRegistered:bool} 

我收到以下错误:

 错误1类型'Key'未定义

有没有办法使用带有记录类型的数据注释?显然,EF Code-First需要注释...

解决方案

记录类型支持属性很好(并且具有您的语法)



检查您对 System.ComponentModel.DataAnnotations 的引用是否正确,那就是 KeyAttribute 被定义。



编辑:EF要使用属性,这就是为什么使用记录t与EF网格良好。您仍然可以通过使用 CLIMutable 属性来标记记录,从而使其工作在F#3.0+中(这将生成属性设置器和无参数构造函数,这些构造函数被C#中心框架所理解,图书馆)



您正在看的文章是用F#2.0写的 - CLIMutable 不是周围还没有办法使用记录。


I am doing a proof of concept in a line of business application where I want to swap out the current C# Code-First Entity Framework implementation with a F# one. I am following this article which seems to work pretty well, but I was hoping to use FSharp record types instead of the classes that the article uses. When I try and add a data annotation to a record type like this:

type Family = {[<Key>]Id:int; LastName:string; IsRegistered:bool}

I get the following error:

Error   1   The type 'Key' is not defined

Is there a way to use data annotations with record types? Apparently, EF Code-First needs annotations...

解决方案

Record types support attributes just fine (and with the syntax you have).

Check if your reference to System.ComponentModel.DataAnnotations is in order, that's where KeyAttribute is defined.

Edit: EF wants to work with properties, that's why using a record doesn't mesh well with EF. You can still make it work in F# 3.0+ by marking the record with CLIMutable attribute (this generates property setters and a parameterless constructor which are taken for granted by C#-centric frameworks and libraries).

The article you're looking at was written with F# 2.0 in mind - CLIMutable wasn't around yet and there was no way of using records for that.

这篇关于FSharp记录类型与实体框架代码优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆