Xamarin表单和EntityFramework属性兼容性 [英] Xamarin Forms and EntityFramework Attributes compatibility

查看:249
本文介绍了Xamarin表单和EntityFramework属性兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端/服务器解决方案使用 C# WPF ASP.NET WebAPI 实体框架。客户端和服务器端口在他的项目中共享模型。现在我试图创建一个新的客户端,使用Xamarin Forms并共享模型,但 Entity Framework属性 MaxLength 索引 NotMapped 等)在PCL 不兼容。所以这是我试过的东西:

I have a client/server solution using C#, WPF, ASP.NET WebAPI and Entity Framework. Client and server clases share the model among his projects. Now I am trying to create a new client, using Xamarin Forms and sharing the model to, but Entity Framework attributes(MaxLength, Index, NotMapped, etc), are not compatible in a PCL. So this are the things that I've tried:

将Microsoft.EntityFrameworkCore导入PCL模型

here 所述应该能够使用Xamarin表单的实体框架,所以我将PCL转换为NetStandard 1.3,它可以工作,允许每个EntityFramework属性。但现在服务器项目与该标准不兼容,我无法在模型项目中添加棱镜和Newtonsoft.Json等软件包。

As described here, you should be able to use entity framework with Xamarin forms, so I convert the PCL to NetStandard 1.3, and it works, every EntityFramework attribute is allowed. But now the server project is not compatible with that standard and I cannot add packages like prism and Newtonsoft.Json in the model project.

模拟Xamarin的属性使用诱饵和切换技巧的形式

我已经尝试了描述这里,基于在模型PCL中创建自定义属性,并在类库中重新定义它们。 MyClient.Droid和MyClient.UWP重新定义了将它们留空的属性,MyServer将使用Entity Framework功能重新定义它们。

I've tried the approach described here, based on creating custom attributes in the model PCL, and redefining them in the class libraries. MyClient.Droid and MyClient.UWP redefine the attributes leaving them empty, and MyServer will redefine them with the Entity Framework functionality.

Custom IndexAttribute - Model PCL:

Custom IndexAttribute - Model PCL:

namespace Model.Compatibility
{
    public class IndexAttribute : Attribute
    {
        public IndexAttribute()
        {
        }
    }
}

自定义IndexAttribute - 服务器端:

Custom IndexAttribute - Server side:

[assembly: TypeForwardedToAttribute(typeof(Model.Compatibility.IndexAttribute))]
namespace Model.Compatibility
{
    public class MockedIndexAttribute : System.ComponentModel.DataAnnotations.Schema.IndexAttribute
    {
        public MockedIndexAttribute()
        {
        }
    }
}

我测试这个aproach调用 var属性= new Model.Compatibility.IndexAttribute(); 。不要调用MockedIndexAttribute构造函数。

I test this aproach calling var attribute = new Model.Compatibility.IndexAttribute();. MockedIndexAttribute constructor is never called.

创建共享项目而不是PCL

这样做有点麻烦,但看起来像是有效的。只需为模型创建一个新的共享项目,并使用这样的条件标志:

This way is a little more messy, but looks like it works. Just creating a new shared project for the model, and using conditional flags like this:

#if !__MOBILE__
[NotMapped, Index]
#endif
public Guid Id { get; set; }

我目前还没有完全部署这种方法,但如果我不能前两种方式工作,我将一起去。

I've not fully deployed this approach at the moment, but if I cannot make none of the first two ways working, I will go with this.

编辑 - 尝试使诱饵和切换属性的方法工作

由于@AdamPedley已建立和线程,我重新定义了IndexAttribute在新的PCL(Xamarin.Compatibility)中,使用与原始命名空间相同的命名空间:

As @AdamPedley sugested and this thread to, I've redefined IndexAttribute in a new PCL(Xamarin.Compatibility), using the same namespace as the original one:

namespace System.ComponentModel.DataAnnotations.Schema
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
    public class IndexAttribute : Attribute
    {
        public IndexAttribute() { }
    }
}

现在,我的PCL模型包含对Xamarin.Compatibility的引用,所以我可以在我的模型属性中使用Index属性:

Now, my PCL Model includes a reference to Xamarin.Compatibility, so I can use Index attribute in my model properties:

[Index]
public Guid Id { get; set; }

然后,从我的Server项目中,我调用下一行代码来检查什么构造函数被调用,自定义属性或由EntityFramework定义的属性:

Then, from my Server project, I call the next line of code to check what constructor is called, the custom attribute, or the one defined by EntityFramework:

PropertyInfo prop = typeof(MyClass).GetProperty("Id");
object[] attributes = prop.GetCustomAttributes(true);

调用的构造函数是自定义的,所以它不起作用,因为它必须调用属性由EntityFramework定义。这是我不知道的东西,根据调用程序集,使我的模型的PCL选择自定义属性或EF属性的机制是什么。

The constructor called is the custom one, so it does not work because it have to call to the attribute defined by EntityFramework. Thats is the thing that I don't know, what is the mechanism that make my model's PCL select custom attribute or EF attribute depending on the calling assembly.

还在我的服务器项目中添加了一个文件,称为TypeForwarding.Net.cs(作为建议的此处),其中包含: p>

I've also added a file in my server project, called TypeForwarding.Net.cs(as sugested here), that contains:

[assembly: TypeForwardedTo(typeof(IndexAttribute))]

但仍然无法使用。

推荐答案

我相信EF流畅的API是PCL和NetStandard友好。因此,您可以创建POCO对象,并使流畅的api执行跨平台映射,而不是使用属性。 msdn.microsoft.com/en-us/library /jj591617(v=vs.113).aspx

I believe the EF fluent API is PCL and NetStandard friendly. Thus you can create POCO objects and let the fluent api do the cross platform mappings instead of using attributes. msdn.microsoft.com/en-us/library/jj591617(v=vs.113).aspx

注意:我使用了一个使用EF6和PCL项目的项目分享MVC / WPF / Mobile

Note: I did this with a project using EF6 and PCL projects to share across MVC / WPF / Mobile

这篇关于Xamarin表单和EntityFramework属性兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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