如何使用可移植类库和EF Code-first? [英] How to work with Portable Class Library and EF Code-first?

查看:66
本文介绍了如何使用可移植类库和EF Code-first?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Windows Phone应用程序,其中在Azure中运行WebApi.

I'm doing an Windows Phone app where I have a WebApi running in Azure.

我正在为我的模型"项目使用新的便携式类库"(http://msdn.microsoft.com/en-us/library/gg597391.aspx),这是我的WebApi项目之间共享的原因(这是一个常规的ASp.NET MVC 4项目)和我的Windows Phone项目.

I'm using the new "Portable Class Library" (http://msdn.microsoft.com/en-us/library/gg597391.aspx) for my "Models" project which is of cause shared between my WebApi project (this is a normale ASp.NET MVC 4 project) and my Windows Phone project.

这很好用,并且可以根据需要对模型(POCO)类进行序列化和反序列化.

This works great and the model (POCO) classes are serialized and deserialized just as I want.

现在,我想开始存储一些Models/POCO对象,并希望使用EF Code-first,但这是一个问题,因为我无法将EntityFramework程序集添加到便携式类库"中项目,实际上我也不想这样做,因为在我的Models项目中只需要一小部分(属性).

Now I want to start storing some of my Models/POCO objects and would like to use EF Code-first for that, but that's kind of a problem as I can't add the EntityFramework assembly to my "Portable Class Library" project, and really I would not like to either as I only need a small part (the attributes) in my Models project.

那么,关于如何采用最佳方法的任何建议?

So, any suggestions to how a approach this the best way?

更新: 好吧,看来我实际上可以将EntityFramework程序集添加到项目中,但这并不能真正帮到我,因为我需要使用的属性存在于System.ComponentModel.DataAnnotations中,而该类在Windows Phone上无法使用. 还有建议吗?

UPDATE: Well, it seems like I can actually add the EntityFramework assembly to the project, but that doesn't really help me, as the attributes I need to use lives in System.ComponentModel.DataAnnotations which can't be used on Windows Phone. Any suggestions still?

推荐答案

不要使用属性.请改用fluent API,并为持久性(EF)创建单独的程序集,该程序集将引用您的模型程序集.持久性程序集将由您的WebAPI层使用.

Don't use attributes. Use fluent API instead and create separate assembly for persistence (EF) which will reference your model assembly. Persistence assembly will be use used by your WebAPI layer.

这篇关于如何使用可移植类库和EF Code-first?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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