如何使用Mono设置Entity Framework/SQL Server [英] How to setup Entity Framework / SQL Server with Mono

查看:106
本文介绍了如何使用Mono设置Entity Framework/SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个非常简单的测试项目,以评估Mono Entity Framework的功能.尝试访问数据时,在使用.NET Runtime时遇到有关解析模型数据的运行时异常.使用Mono运行时时,会引发stackoverflow异常.

I'm trying to setup a very simple test project to evaluate Mono Entity Framework capabilities. When trying to access data, I get a runtime exception about parsing the model data when using .NET Runtime. When using Mono runtime, a stackoverflow exception is thrown.

这似乎是一个内部Mono错误,在编译CSDL Schema Version 3时会发生-请参见 Mono Bugtracker .

This seems to be an internal mono bug which occurs when compiling the CSDL Schema Version 3 - see Mono Bugtracker.

所以我的问题是:

如何使用实体框架设置Mono以与Microsoft SQL Server一起使用?由于我找不到太多信息,这是否有可能?你们中是否有人成功使用EF成功设置了单声道解决方案,并遇到了类似的错误?

How do I setup Mono with Entity Framework to work with a Microsoft SQL Server? Since I don't find much information, should this even be possible? Did anyone of you successfully setup a mono solution using EF and came across similar errors?

这是我到目前为止所做的:

This is what I have done so far:

  • 已安装Mono 3.0.1 Beta(最新的稳定版不包含EntityFramework.dll)
  • 为Visual Studio设置 Mono配置文件
  • 已针对延迟签名的组件禁用了强名验证 EntityFramework.dll/EntityFramework.SQLServer.dll并将它们添加到GAC中,以防止运行时错误
  • 创建了一个简单的模型并添加了代码生成项
  • 使用模型编写代码
  • Installed Mono 3.0.1 Beta (latest stable doesn't include EntityFramework.dll)
  • Setup a Mono Profile for Visual Studio
  • Disabled strong name verification for delay signed assembilies for EntityFramework.dll / EntityFramework.SQLServer.dll and added them to the GAC to prevent runtime errors
  • Created a simple model + added Code Generation Items
  • written code using the model

该代码可与Microsoft EntityFramework.dll(版本6,预发行版)一起使用.当使用Mono等效项时,在解析实体模型时似乎存在问题:

The code works with Microsoft EntityFramework.dll (Version 6, Prerelease). When using the Mono equivalent, it looks like there are problems while parsing the entity model:

异常消息(.NET运行时):

Exception message (.NET Runtime):

对象引用未设置为对象的实例.

Stacktrace(.NET运行时):

Stacktrace (.NET Runtime):

   at System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
   at System.Xml.XmlTextReaderImpl..ctor(String url, Stream input, XmlNameTable nt)
   at System.Xml.XmlTextReader..ctor(Stream input)
   at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.SomSchemaSetHelper.AddXmlSchemaToSet(XmlSchemaSet schemaSet, XmlSchemaResource schemaResource, HashSet`1 schemasAlreadyAdded)
   at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.SomSchemaSetHelper.AddXmlSchemaToSet(XmlSchemaSet schemaSet, XmlSchemaResource schemaResource, HashSet`1 schemasAlreadyAdded)
   at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.SomSchemaSetHelper.ComputeSchemaSet(SchemaDataModelOption dataModel)
   at System.Data.Entity.Core.Common.Utils.Memoizer`2.Result.GetValue()
   at System.Data.Entity.Core.Common.Utils.Memoizer`2.Evaluate(TArg arg)
   at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.SomSchemaSetHelper.GetSchemaSet(SchemaDataModelOption dataModel)
   at System.Data.Entity.Core.EntityModel.SchemaObjectModel.Schema.Parse(XmlReader sourceReader, String sourceLocation)
   at System.Data.Entity.Core.EntityModel.SchemaObjectModel.SchemaManager.ParseAndValidate(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModel, AttributeValueNotification providerNotification, AttributeValueNotification providerManifestTokenNotification, ProviderManifestNeeded providerManifestNeeded, IList`1& schemaCollection)
   at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Loader.LoadItems(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths)
   at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Init(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError, DbProviderManifest& providerManifest, DbProviderFactory& providerFactory, String& providerManifestToken, Memoizer`2& cachedCTypeFunction)
   at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection..ctor(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths)
   at System.Data.Entity.Core.Metadata.Edm.MetadataCache.StoreMetadataEntry.LoadStoreCollection(EdmItemCollection edmItemCollection, MetadataArtifactLoader loader)
   at System.Data.Entity.Core.Metadata.Edm.MetadataCache.LoadItemCollection[T](IItemCollectionLoader`1 itemCollectionLoader, T entry)
   at System.Data.Entity.Core.Metadata.Edm.MetadataCache.GetOrCreateStoreAndMappingItemCollections(String cacheKey, MetadataArtifactLoader loader, EdmItemCollection edmItemCollection, Object& entryToken)
   at System.Data.Entity.Core.EntityClient.EntityConnection.LoadStoreItemCollections(MetadataWorkspace workspace, DbConnection storeConnection, DbConnectionOptions connectionOptions, EdmItemCollection edmItemCollection, MetadataArtifactLoader artifactLoader)
   at System.Data.Entity.Core.EntityClient.EntityConnection.GetMetadataWorkspace(Boolean initializeAllCollections)
   at System.Data.Entity.Core.EntityClient.EntityConnection.InitializeMetadata(DbConnection newConnection, DbConnection originalConnection, Boolean closeOriginalConnectionOnFailure)
   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection()
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<GetEnumerator>m__2C3()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at EntityFrameworkMono.Program.Main(String[] args) in c:\Users\Christopher Dresel\Documents\Visual Studio 2012\Projects\New\EntityFrameworkMono\EntityFrameworkMono\Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart() 

异常消息(单声道运行时):

Exception message (Mono Runtime):

请求的操作导致堆栈溢出.

Stacktrace(单声道运行时):

Stacktrace (Mono Runtime):

  at System.Xml.Schema.XmlSchemaGroupBase.CheckRecursion (Int32 depth, System.Xml.Schema.ValidationEventHandler h, System.Xml.Schema.XmlSchema schema) [0x00000] in <filename unknown>:0 
  at System.Xml.Schema.XmlSchemaGroupBase.CheckRecursion (Int32 depth, System.Xml.Schema.ValidationEventHandler h, System.Xml.Schema.XmlSchema schema) [0x00000] in <filename unknown>:0 
  at System.Xml.Schema.XmlSchemaElement.CheckRecursion (Int32 depth, System.Xml.Schema.ValidationEventHandler h, System.Xml.Schema.XmlSchema schema) [0x00000] in <filename unknown>:0 
  at System.Xml.Schema.XmlSchemaGroupBase.CheckRecursion (Int32 depth, System.Xml.Schema.ValidationEventHandler h, System.Xml.Schema.XmlSchema schema) [0x00000] in <filename unknown>:0 
  at System.Xml.Schema.XmlSchemaGroupBase.CheckRecursion (Int32 depth, System.Xml.Schema.ValidationEventHandler h, System.Xml.Schema.XmlSchema schema) [0x00000] in <filename unknown>:0 
  at System.Xml.Schema.XmlSchemaElement.CheckRecursion (Int32 depth, System.Xml.Schema.ValidationEventHandler h, System.Xml.Schema.XmlSchema schema) [0x00000] in <filename unknown>:0 
  at System.Xml.Schema.XmlSchemaGroupBase.CheckRecursion (Int32 depth, System.Xml.Schema.ValidationEventHandler h, System.Xml.Schema.XmlSchema schema) [0x00000] in <filename unknown>:0 
  at System.Xml.Schema.XmlSchemaGroupBase.CheckRecursion (Int32 depth, System.Xml.Schema.ValidationEventHandler h, System.Xml.Schema.XmlSchema schema) [0x00000] in <filename unknown>:0
...

这是我正在使用的代码:

This is the code I'm using:

CustomerEntities entities = new CustomerEntities();
var count = entities.Customers.Count(); // Exception throws here

Console.WriteLine(count);
Console.ReadKey();

EDMX文件

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
    <!-- EF Runtime content -->
    <edmx:Runtime>
        <!-- SSDL content -->
        <edmx:StorageModels>
            <Schema Namespace="CustomerModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
                <EntityContainer Name="CustomerModelStoreContainer">
                    <EntitySet Name="Customer" EntityType="CustomerModel.Store.Customer" store:Type="Tables" Schema="dbo" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" />
                </EntityContainer>
                <EntityType Name="Customer">
                    <Key>
                        <PropertyRef Name="CustomerID" />
                    </Key>
                    <Property Name="CustomerID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
                    <Property Name="CustomerName" Type="nvarchar" Nullable="false" MaxLength="50" />
                </EntityType>
            </Schema>
        </edmx:StorageModels>
        <!-- CSDL content -->
        <edmx:ConceptualModels>
            <Schema Namespace="CustomerModel" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns="http://schemas.microsoft.com/ado/2009/11/edm" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
                <EntityContainer Name="CustomerEntities" annotation:LazyLoadingEnabled="true">
                    <EntitySet Name="Customers" EntityType="CustomerModel.Customer" />
                </EntityContainer>
                <EntityType Name="Customer">
                    <Key>
                        <PropertyRef Name="CustomerID" />
                    </Key>
                    <Property Name="CustomerID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
                    <Property Name="CustomerName" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" />
                </EntityType>
            </Schema>
        </edmx:ConceptualModels>
        <!-- C-S mapping content -->
        <edmx:Mappings>
            <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
                <EntityContainerMapping StorageEntityContainer="CustomerModelStoreContainer" CdmEntityContainer="CustomerEntities">
                    <EntitySetMapping Name="Customers">
                        <EntityTypeMapping TypeName="CustomerModel.Customer">
                            <MappingFragment StoreEntitySet="Customer">
                                <ScalarProperty Name="CustomerID" ColumnName="CustomerID" />
                                <ScalarProperty Name="CustomerName" ColumnName="CustomerName" />
                            </MappingFragment>
                        </EntityTypeMapping>
                    </EntitySetMapping>
                </EntityContainerMapping>
            </Mapping>
        </edmx:Mappings>
    </edmx:Runtime>
</edmx:Edmx>

最后是我正在使用的连接字符串:

And finally the connection string I'm using:

<connectionStrings>
    <add name="CustomerEntities"
         connectionString="metadata=.\CustomerModel.csdl|.\CustomerModel.ssdl|.\CustomerModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=CHRISTOPHERPC\SQLExpress;initial catalog=test;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient"/>
</connectionStrings>

推荐答案

Dresel,由于我的声誉可能在此站点上存在误导,我可以肯定地说,此时不建议将EntityFramework与mono一起使用.尽管从技术上来说受支持",但大多数库充其量是无法正常运行的.在花了大约一个月的时间进行深入挖掘之后,我无法确定将某个功能完整的站点制作到%20以上的可能性.我改为使用NHibernate,并对结果感到兴奋.并建议您进行同样的操作,直到EF Mono项目的基础进一步巩固为止. Nhibernate示例(使用MySQL,支持MSSQL)

Dresel, misleading as my reputation may be on this site, I can safely say that using the EntityFramework with mono is not the recommended approach at this time. Although it is technically "supported", the majority of the library is non functional at best. After spending about a month digging as far as I could go, I was unable to peg the possibility of making a fully functioning site above %20. I instead used NHibernate and have been thrilled with the results. And recommend doing the same until the base has solidified further for the EF Mono project. Nhibernate Example (using MySQL, MSSQL supported)

我希望这至少可以有所帮助,因为这只是我真诚的想法.无论您选择做什么,都祝您好运,我知道MS Opensource的这一举动会变得非常包罗万象"

I hope this helps at least a little bit as it's only my sincere thought. Good luck on whatever you choose to do, I know this MS Opensource move can get very "roadblocky"

这篇关于如何使用Mono设置Entity Framework/SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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