How to locate the meta files generated by EF in a class library?

僤鯓⒐⒋嵵緔 提交于 2019-12-24 02:55:23

问题


I've moved my Entity Framework 4 model to a class library. The meta files are built to \bin\Debug

What is the connection string I use to locate the meta files in the class library? I have tried:

  <add name="Models.DataModelConnectionString" 
connectionString="metadata=res://MyClassLibrary, 1.0.0.0, neutral,
 null/bin/Debug/Models.DataModel.csdl|res://MyClassLibrary, 1.0.0.0, neutral,
 null/bin/Debug/Models.DataModel.ssdl|res://MyClassLibrary, 1.0.0.0, neutral,
 null/bin/Debug/Models.DataModel.msl;provider=Devart.Data.Oracle;
....">

I still get the error "The specified metadata path is not valid".

Do I need to move the meta files to the class library root directory?

Edit I have tried using res://*/Models.DataModel.csdl etc. but got the error "Unable to load the specified metadata resource." instead


回答1:


When the EF model is located in a separate/project (MyCompany.MyApp.Infra) which is used by another project, you need to correctly configure the connection string in the app.config or web.config

See this example:

MyContext.cs in MyCompany.MyApp.Infra project.

namespace MyCompany.MyApp.Domain
{
    using System.Data.Objects;

    /// <summary>
    /// The MyContext
    /// </summary>
    public partial class MyContext : ObjectContext, IUnitOfWork
    {
        /// <summary>
        /// The ConnectionString
        /// </summary>
        public const string ConnectionString = "name=MyContext";

        /// <summary>
        /// The ContainerName
        /// </summary>
        public const string ContainerName = "MyContext";

        /// <summary>
        /// Initializes a new instance of the <see cref="MyContext"/> class.
        /// </summary>
        public MyContext()
            : base(ConnectionString, ContainerName)
        {
            this.ContextOptions.LazyLoadingEnabled = true;
        }
    }
}

web.config in Silverlight Web Project

<add name="MyContext"
connectionString="metadata=res://MyCompany.MyApp.Infra/DataModel.MyContext.csdl|
                           res://MyCompany.MyApp.Infra/DataModel.MyContext.ssdl|
                           res://MyCompany.MyApp.Infra/DataModel.MyContext.msl;
provider=System.Data.SqlClient;
provider connection string=&quot;Data Source=localhost\DB_01;Initial Catalog=MyDB;Persist Security Info=True;User ID=usr;Password=pwd;MultipleActiveResultSets=True&quot;"
providerName="System.Data.EntityClient"
/>



回答2:


The solution is to embed the metadata files in the assembly by changing the output from CopyToOutputDirectory to EmbedInOutputAssembly

This would allow res://*/Model.ssdl etc. to work



来源:https://stackoverflow.com/questions/6450682/how-to-locate-the-meta-files-generated-by-ef-in-a-class-library

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