gpt4 book ai didi

entity-framework - 如何使用 Mono 设置 Entity Framework /SQL Server

转载 作者:行者123 更新时间:2023-12-04 12:22:59 25 4
gpt4 key购买 nike

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

这似乎是编译 CSDL 模式版本 3 时发生的内部单声道错误 - 参见 Mono Bugtracker .

所以我的问题是:

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

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

  • 已安装 Mono 3.0.1 Beta(最新稳定版不包括 EntityFramework.dll)
  • 设置 Mono Profile适用于 Visual Studio
  • Disabled strong name verification for delay signed assembilies为了EntityFramework.dll/EntityFramework.SQLServer.dll并将它们添加到 GAC 以防止运行时错误
  • 创建了一个简单的模型 + 添加了代码生成项
  • 使用模型编写的代码

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

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

    Object reference not set to an instance of an object.



    堆栈跟踪(.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()

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

    The requested operation caused a stack overflow.



    堆栈跟踪(单声道运行时):
      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>

    最佳答案

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

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

    关于entity-framework - 如何使用 Mono 设置 Entity Framework /SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13624065/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com