与参数的构造函数记录? [英] Record with parameterless constructor?

查看:80
本文介绍了与参数的构造函数记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建一个使用实体框架的Web应用程序(ASP.NET MVC3),我已经再次碰了壁。尝试运行在视图上集合foreach循环时,它抛出以下异常:

I'm trying to build a web app (ASP.NET MVC3) that uses Entity Framework, and I've once again hit a wall. It throws following exception when trying to run a foreach loop over the collection in the view:

System.InvalidOperationException:本
  类的GvG.Entities.News'没有
  参数的构造函数。

System.InvalidOperationException: The class 'GvG.Entities.News' has no parameterless constructor.

现在是我的问题,是否有可能在我的记录类型定义莫名其妙参数构造函数?

Now is my question, is it possible to somehow define a parameterless constructor on my record type?

在此刻我的记录类型如下:

My record type at the moment looks like:

type News = { 
    mutable ID:int; 
    mutable Author:string; 
    mutable Title:string; 
    mutable Content:string }

我所知道的,我可以创建烘烤等领域,而不是一类,但多数民众赞成什么,我试图避免的。

I'm aware of that I can create a class with baking-fields etc. instead, but thats what I'm trying to avoid.

推荐答案

我尝试了一些承包项目我做了F#的团队在解决这个(espeically为实体框架),你可以找到的 F#PowerPack的来源。它不完全测试&放大器;你必须建立它自己。实验解决方案取代了与其他(可变)类型查询的所有F#元组和F#记录,然后转换结果返回给记录/元组。

I tried to solve this (espeically for Entity Framework) during some contracting project I did for the F# team and you can find an experimental solution in F# PowerPack sources. It is not fully tested & you'd have to build it yourself. The experimental solution replaces all F# tuples and F# records in the query with other (mutable) types and then transforms results back to records/tuples.

修改没看到你的问题定义类提,但我会离开这里的例子为别人谁可能来这里与同样的问题。

EDIT Didn't see the mention about defining class in your question, but I'll leave the example here for others who may come here with the same issue.

有没有简单的解决方法。不幸的解决方案是定义明确的具有属性的类:

There is no easy workaround. The unfortunate solution is to define a class with properties explicitly:

type News() =
  let mutable id = 0
  let mutable author = ""
  let mutable title = ""
  let mutable content = ""
  member x.ID with get() = id and set(v) = id <- v
  member x.Author with get() = author and set(v) = author <- v
  member x.Title with get() = title and set(v) = title <- v
  member x.Content with get() = content and set(v) = content <- v

相比,记录这是非常丑陋的,但它是做在F#的当前版本的唯一途径。这是一些在F#团队知道,所以有可能会在下一版本的一些更好的解决方案。

That's very ugly compared to records, but it's the only way to do it in the current version of F#. It is something that the F# team is aware of, so there may be some better solution in the next version.

这篇关于与参数的构造函数记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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