How to work with Portable Class Library and EF Code-first?

我只是一个虾纸丫 提交于 2019-12-05 20:25:50

问题


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

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.

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

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?

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?


回答1:


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.




回答2:


I use a modified approach than Mikkel Hempel's, without the need to use pre processing directives.

  1. Create a standard .NET class library, call it Models
  2. Create a partial class representing what you want to be shared

    public partial class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
  3. For non-portable code (like DataAnnotations), create another partial class and use Metadata

    [MetadataTypeAttribute(typeof(Person.Metadata))]
    public partial class Person
    {
        internal sealed class Metadata
        {
            private Metadata() { } // Metadata classes shouldn't be instantiated
    
            // Add metadata attributes to perform validation
            [Required]
            [StringLength(60)]
            public string Name;
        }
    }
    
  4. Create a Portable Class Library, and add the class from step 2 "As Link"




回答3:


When I need my domain-project across multiple platforms, I usually:

  1. Create the standard .NET-class library project for the domain code
  2. For each platform I create a platform specific class library
  3. For each platform specific class library I add the files from the standard .NET-class library as links (Add existing files -> As link) and hence they're updated automatically when you edit either the linked file or the original file.
  4. When I add a new file to the .NET-class library, I add it as links to the platform specific class libraries.
  5. Platform specific attributes (i.e. Table and ForeignKey which is a part of the DataAnnotations-assembly) can be opted out using the pre-processor tags. Lets say I have a .NET-class library with a class and a Silverlight-project with the linked file, then I can include the .NET-specific attributes by doing:

    #if !SILVERLIGHT
    [Table("MyEntityFrameworkTable")]
    #endif 
    public class MyCrossPlatformClass 
    {
        // Blah blah blah
    }
    

and only include the DataAnnotations-assembly in the .NET-class library.

I know it's more work than using the Portable Class Library, but you can't opt out attributes in a PCL like in the example above, since you're only allowed to reference shared assemblies (which again DataAnnotations is not).



来源:https://stackoverflow.com/questions/11917736/how-to-work-with-portable-class-library-and-ef-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!