【问题标题】:How to setup Entity Framework / SQL Server with Mono如何使用 Mono 设置实体框架/SQL Server
【发布时间】:2012-11-29 10:49:14
【问题描述】:

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

这似乎是编译 CSDL 架构版本 3 时出现的内部单声道错误 - 请参阅 Mono Bugtracker

所以我的问题是:

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

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

该代码适用于 Microsoft EntityFramework.dll(版本 6,预发布)。使用 Mono 等效项时,解析实体模型时似乎存在问题:

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

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

堆栈跟踪(.NET 运行时):

   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() 

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

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

Stacktrace(单声道运行时):

  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
...

这是我正在使用的代码:

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>

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

<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>

【问题讨论】:

  • 您是否尝试过 Mono 2.11.3 或更高版本?
  • 我用 go-mono.com/mono-downloads/download.html 的 Mono 3.0.1 Beta 试用了它 - 现在有一个新版本 (3.0.2),我会用这个版本再试一次并发布我的结果。
  • 使用 3.0.2 时出现同样的错误...
  • 错误在 EF 中 - 有关详细信息,请参阅我的答案。

标签: entity-framework mono


【解决方案1】:

Dresel,由于我在这个网站上的声誉可能会产生误导,我可以肯定地说,目前不推荐使用带有 mono 的 EntityFramework 方法。尽管它在技术上受到“支持”,但该库的大部分内容充其量是无法使用的。在花了大约一个月的时间尽可能地挖掘之后,我无法确定将一个功能齐全的网站建立在 %20 以上的可能性。我改为使用 NHibernate,并且对结果感到非常兴奋。并建议在 EF Mono 项目的基础进一步巩固之前也这样做。 Nhibernate Example (using MySQL, MSSQL supported)

我希望这至少会有所帮助,因为这只是我的真诚想法。无论你选择做什么,祝你好运,我知道这个 MS 开源举措可能会变得非常“障碍”

【讨论】:

  • 只是好奇——当你说“非功能库”时,你指的是最新的单声道版本(3.x)吗?
  • 我的团队和我自己在 12 月底运行 Ubuntu 和所有最新软件。它们是在 windows 中编译的库,但当时不支持某些需要的东西。
  • 但是您能够让 EF 运行吗?正如您在我的帖子中所读到的,我什至无法做到这一点——而且也没有关于这个主题的资源(至少我没有找到)。因此,如果您对如何做到这一点有任何暗示,或者可以告诉我我缺少什么,我将非常感激。
  • 我最终确实得到了一个简单的 CRUD 页面,它可以打开和关闭,我不再拥有该项目,否则我会将其发送给您。然而,这是非常不一致的。以至于如果一件事执行得很快,什么都不会起作用。
  • 虽然我个人还没有再次使用单声道的 EF,但是我认为它现在可以正常工作了。 EF6 完全开源/受支持。如果您要使用 MySQL,我的理解是您需要一个商业连接器。如果我可以帮助任何其他特定情况,请告诉我。我可能需要重新访问/删除这个问题。编辑 2:我仍然会推荐 NHibernate %100,因为它有大约 8 年的领先优势。
猜你喜欢
  • 2013-07-13
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-23
相关资源
最近更新 更多