BreezeJs DataModal使用EF作为设计工具

时间:2013-12-19 23:14:08

标签: entity-framework breeze

我在项目的EF as Design tool资源上使用资源。我的解决方案中有三个不同的项目。

DTO - 具有DbContext和DTO对象和方法,用于返回元数据,如示例中所述。

服务 - 具有WebApi方法并从DTO项目调用getmetaData并将字符串返回为 -

[BreezeController]
public class PersonController : ApiController
{
    [HttpGet]
    public string MetaData()
    {          
        return new LifeAudit_DTO.DtoMetaData().MetaData();
    }
}

3。 WebApi项目的第三个项目(使用vs2013)有路由信息和引用服务库,这是我的主界面(UI)项目。

  1. 我正在使用EF6和Nuget Project的最新Breeze。
  2. 我将Database init设置为null,以下是我的Datacontext代码 -

    internal class LifeAuditDbContext : DbContext
    {
     static LifeAuditDbContext()
     {
        Database.SetInitializer<LifeAuditDbContext>(null);
     }
     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
        modelBuilder.Configurations.Add(new PersonDtoConfiguration());            
      }
    public DbSet<Person> Person { get; set; }
     }
    
  3. 在致电服务时,我收到以下错误 -

    { $ id:&#34; 1&#34;, $ type:&#34; System.Web.Http.HttpError,System.Web.Http&#34;, 消息:&#34;发生错误。&#34;, ExceptionMessage:&#34;从数据库获取提供程序信息时发生错误。这可能是由实体框架使用不正确的连接字符串引起的。检查内部异常以获取详细信息,并确保连接字符串正确。&#34;, ExceptionType:&#34; System.Data.Entity.Core.ProviderIncompatibleException&#34;, StackTrace:&#34;在System.Web.Http.ApiController.d_ 1.MoveNext()---从抛出异常的上一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)at at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()", InnerException: { $id: "2", $type: "System.Web.Http.HttpError, System.Web.Http", Message: "An error has occurred.", ExceptionMessage: "The provider did not return a ProviderManifestToken string.", ExceptionType: "System.Data.Entity.Core.ProviderIncompatibleException", StackTrace: " at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)", InnerException: { $id: "3", $type: "System.Web.Http.HttpError, System.Web.Http", Message: "An error has occurred.", ExceptionMessage: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)", ExceptionType: "System.Data.SqlClient.SqlException", StackTrace: " at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction)在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose)at at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo,SqlInternalConnectionTds connHandler,Boolean ignoreSniOpenTimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity,Boolean withFailover)at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword) System.SData.SqlCl,SecureString newSecurePassword,Boolean ignoreSniOpenTimeout,TimeoutTimer timeout,Boolean withFailover) System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist中的ient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,String newPassword,SecureString newSecurePassword,Boolean redirectedUserInstance,SqlConnectionString connectionOptions,SqlCredential credential,TimeoutTimer timeout)(TimeoutTimer超时,SqlConnectionString connectionOptions,SqlCredential凭证,String newPassword, SecureString newSecurePassword,Boolean redirectedUserInstance)在System.Data的System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity标识,SqlConnectionString connectionOptions,SqlCredential凭证,Object providerInfo,String newPassword,SecureString newSecurePassword,Boolean redirectedUserInstance,SqlConnectionString userConnectionOptions,SessionData reconnectSessionData)。 SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,DbConnectionPool池,DbConnectio System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal)上的System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool池,DbConnection owningObject,DbConnectionOptions选项,DbConnectionPoolKey poolKey,DbConnectionOptions userOptions)中的n owningConnection,DbConnectionOptions(userOptions) System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&amp;连接)在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(的DbConnection owningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource 1重试,DbConnectionOptions USEROPTIONS,DbConnectionInternal oldConnection,DbConnectionInternal&安培;连接)在System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(的DbConnection outerConnection,DbConnectionFactory在System.Data的System.Data.SqlClient.SqlConnection.Open()处的System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1重试)的connectionFactory,TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1次重试,DbConnectionOptions userOptions) System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy中的.Entity.SqlServer.SqlProviderServices。&lt;&gt; c _DisplayClass2f.b_ 2d()。&lt;&gt; c _DisplayClass1.b__0() System.Data上的System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func 1 operation) at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) at System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action 1 act)at System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection,Action`1 act) .EN System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)&#34;中的tity.SqlServer.SqlProviderServices.GetDbProviderManifestToken(DbConnection连接)。 } } }

  4. 看起来它尝试使用连接EF,但我传递null。

    非常感谢任何帮助。

    谢谢, 达尔吉特

1 个答案:

答案 0 :(得分:1)

您向我们展示了您的真实 DbContext。您没有向我们展示仅为DTO元数据生成而存在的 phony DbContext

我期待像

这样的东西
// notice the "Dto" in the class name
internal class LifeAuditDbDtoContext : DbContext {

    // class constructor
    static LifeAuditDbDtoContext()
    {
        Database.SetInitializer<LifeAuditDbDtoContext>(null);
    }

    // instance constructor
    public LifeAuditDbDtoContext() : base("Dummy") {
    /// stuff
    }
}

这个Dto版本是DbContext,肯定需要null初始化程序!

仍然无法使用?

有时我认为即使初始值设定为null,我也必须给EF一个虚假的连接字符串。

  

我不知道为什么,这可能是伏都教。

试试这个并告诉我们它是否适合你(请注意字符串的名称和db是“dummy”。虚拟数据库不应该存在也不应该创建):

<connectionStrings>
    <add name="Dummy" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\dummy.mdf;Initial Catalog=dummy;Integrated Security=True" providerName="System.Data.SqlClient" />

    ... other real strings ...

当然,您可以提供真正的连接字符串名称,并免除虚假。 null初始化程序确保应用程序不会重新初始化数据库。但就个人而言,如果EF正在寻找这个虚拟DbContext的数据库连接字符串,我不希望它靠近我的真实数据库。

相关问题