模拟实体框架数据库

发布于 2024-11-08 23:21:13 字数 147 浏览 0 评论 0原文

我正在使用实体框架 4+。

是否可以使用一些随机数据从模式自动创建一些 MOCK 数据库?在哪里放置整数,在哪里放置整数,在哪里放置字符串,放置一些 GUID 或其他什么......

这将帮助我调试我的应用程序很多。

谢谢你,詹姆斯

I am using Entity Framework 4+.

Is it possible to automatically create some MOCK database from the schema with some random data? Where ints, put ints, where strings, put some GUIDs or whatever...

It would help me to debug my application a lot.

Thank you, James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

临走之时 2024-11-15 23:21:13

我发现这个很棒的工具可以与 EF 4 一起使用进行模拟数据库。基本上,它只是一个 T4 模板,它创建“普通”EF 层,还创建可用于测试的模拟对象和接口。更多文档此处

不过,有一点需要注意。开箱即用的它对我不起作用,因为我的存储库代码调用 SaveChanges() 和其他未在生成的接口中实现的方法。我能够通过从 Microsoft 的 ObjectContext 实现中提取接口并创建我自己的 IObjectContext 接口来解决这个问题。然后,我创建了一个基类(用于模拟),通过简单地将每个调用委托给注入的模拟来实现此接口。这样我就可以使用 Moq 在我的测试类中创建该部分,同时仍然保留在生成的模拟中跟踪插入、更新和删除的能力。

以下是我对“Context”T4 模板的更改,以解决此问题。抱歉,我是零碎地做这件事 - 所以没有让我发布整个代码清单,因为它太长了。

fileManager.Process(); 之前添加此代码

fileManager.StartNewFile( "IObjectContext.cs");
WriteHeader();
WriteHeaderIncludeSystem();
WriteHeaderIncludeData();
WriteHeaderIncludeContainers();
WriteNamespaceBegin( code, namespaceName );
WriteObjectContextInterface( container, code );
WriteNamespaceEnd( namespaceName );

fileManager.StartNewFile( container.Name + "Mock.ObjectContext.cs");
WriteHeader();
WriteHeaderIncludeSystem();
WriteHeaderIncludeData();
WriteHeaderIncludeContainers();
WriteNamespaceBegin( code, namespaceName );
WriteObjectContextMockBase( container, code );
WriteNamespaceEnd( namespaceName );

WriteInterface() 块之后添加此代码。

<#+
void WriteObjectContextInterface( EntityContainer container, CodeGenerationTools code )
{
#>
/// <summary>
/// The interface for the generic object context. This contains all of
/// the <code>ObjectContext</code> properties that are implemented in the 
/// concrete ObjectContext class. This interface was created so these members
/// can be mocked, as ObjectContext doesn't have a default public constructor.
/// </summary>
<#=Accessibility.ForType(container)#> interface IObjectContext : IDisposable
{
    void AcceptAllChanges();
    void AddObject(string entitySetName, object entity);
    TEntity ApplyCurrentValues<TEntity>(string entitySetName, TEntity currentEntity) where TEntity : class;
    TEntity ApplyOriginalValues<TEntity>(string entitySetName, TEntity originalEntity) where TEntity : class;
    void ApplyPropertyChanges(string entitySetName, object changed);
    void Attach(System.Data.Objects.DataClasses.IEntityWithKey entity);
    void AttachTo(string entitySetName, object entity);
    int? CommandTimeout { get; set; }
    DbConnection Connection { get; }
    ObjectContextOptions ContextOptions { get; }
    void CreateDatabase();
    string CreateDatabaseScript();
    EntityKey CreateEntityKey(string entitySetName, object entity);
    T CreateObject<T>() where T : class;
    ObjectSet<TEntity> CreateObjectSet<TEntity>() where TEntity : class;
    ObjectSet<TEntity> CreateObjectSet<TEntity>(string entitySetName) where TEntity : class;
    void CreateProxyTypes(IEnumerable<Type> types);
    ObjectQuery<T> CreateQuery<T>(string queryString, params ObjectParameter[] parameters);
    bool DatabaseExists();
    string DefaultContainerName { get; set; }
    void DeleteDatabase();
    void DeleteObject(object entity);
    void Detach(object entity);
    void DetectChanges();
    void Dispose();
    int ExecuteFunction(string functionName, params ObjectParameter[] parameters);
    ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, params ObjectParameter[] parameters);
    ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, MergeOption mergeOption, params ObjectParameter[] parameters);
    int ExecuteStoreCommand(string commandText, params object[] parameters);
    ObjectResult<TElement> ExecuteStoreQuery<TElement>(string commandText, params object[] parameters);
    ObjectResult<TEntity> ExecuteStoreQuery<TEntity>(string commandText, string entitySetName, MergeOption mergeOption, params object[] parameters);
    object GetObjectByKey(System.Data.EntityKey key);
    void LoadProperty(object entity, string navigationProperty);
    void LoadProperty(object entity, string navigationProperty, MergeOption mergeOption);
    void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector);
    void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector, MergeOption mergeOption);
    System.Data.Metadata.Edm.MetadataWorkspace MetadataWorkspace { get; }
    ObjectStateManager ObjectStateManager { get; }
    void Refresh(RefreshMode refreshMode, IEnumerable collection);
    void Refresh(RefreshMode refreshMode, object entity);
    int SaveChanges();
    int SaveChanges(bool acceptChangesDuringSave);
    int SaveChanges(SaveOptions options);
    ObjectResult<TElement> Translate<TElement>(DbDataReader reader);
    ObjectResult<TEntity> Translate<TEntity>(DbDataReader reader, string entitySetName, MergeOption mergeOption);
    bool TryGetObjectByKey(EntityKey key, out object value);
}
<#+
}
#>

<#+
void WriteObjectContextMockBase( EntityContainer container, CodeGenerationTools code )
{
#>
/// <summary>
/// The default concrete implementation of IObjectContext that will be used for mocking. 
/// This contains all of the <code>IObjectContext</code> members that are implemented in the 
/// concrete ObjectContext class. This class was created so these members
/// can be mocked.
/// </summary>
<#=Accessibility.ForType(container)#> abstract class ObjectContextMockBase : IObjectContext
{
    private readonly IObjectContext _objectContext;
    public ObjectContextMockBase(IObjectContext objectContext)
    {
        if (objectContext == null)
            throw new System.ArgumentNullException("objectContext");
        _objectContext = objectContext;
    }

    public virtual void AcceptAllChanges()
    {
        _objectContext.AcceptAllChanges();
    }

    public virtual void AddObject(string entitySetName, object entity)
    {
        _objectContext.AddObject(entitySetName, entity);
    }

    public virtual TEntity ApplyCurrentValues<TEntity>(string entitySetName, TEntity currentEntity) 
        where TEntity : class
    {
        return _objectContext.ApplyCurrentValues<TEntity>(entitySetName, currentEntity);
    }

    public virtual TEntity ApplyOriginalValues<TEntity>(string entitySetName, TEntity originalEntity) 
        where TEntity : class
    {
        return ApplyOriginalValues<TEntity>(entitySetName, originalEntity);
    }

    public virtual void ApplyPropertyChanges(string entitySetName, object changed)
    {
        _objectContext.ApplyPropertyChanges(entitySetName, changed);
    }

    public virtual void Attach(System.Data.Objects.DataClasses.IEntityWithKey entity)
    {
        _objectContext.Attach(entity);
    }

    public virtual void AttachTo(string entitySetName, object entity)
    {
        _objectContext.AttachTo(entitySetName, entity);
    }

    public virtual int? CommandTimeout
    {
        get { return _objectContext.CommandTimeout; }
        set { _objectContext.CommandTimeout = value; }
    }

    public virtual DbConnection Connection 
    { 
        get { return _objectContext.Connection; }
    }

    public virtual ObjectContextOptions ContextOptions
    { 
        get { return _objectContext.ContextOptions; }
    }

    public virtual void CreateDatabase()
    {
        _objectContext.CreateDatabase();
    }

    public virtual string CreateDatabaseScript()
    {
        return _objectContext.CreateDatabaseScript();
    }

    public virtual EntityKey CreateEntityKey(string entitySetName, object entity)
    {
        return _objectContext.CreateEntityKey(entitySetName, entity);
    }

    public virtual T CreateObject<T>() 
        where T : class
    {
        return _objectContext.CreateObject<T>();
    }

    public virtual ObjectSet<TEntity> CreateObjectSet<TEntity>()
        where TEntity : class
    {
        return _objectContext.CreateObjectSet<TEntity>();
    }

    public virtual ObjectSet<TEntity> CreateObjectSet<TEntity>(string entitySetName) 
        where TEntity : class
    {
        return _objectContext.CreateObjectSet<TEntity>(entitySetName);
    }

    public virtual void CreateProxyTypes(IEnumerable<Type> types)
    {
        _objectContext.CreateProxyTypes(types);
    }

    public virtual ObjectQuery<T> CreateQuery<T>(string queryString, params ObjectParameter[] parameters)
    {
        return _objectContext.CreateQuery<T>(queryString, parameters);
    }

    public virtual bool DatabaseExists()
    {
        return _objectContext.DatabaseExists();
    }

    public virtual string DefaultContainerName
    {
        get { return _objectContext.DefaultContainerName; }
        set { _objectContext.DefaultContainerName = value; }
    }

    public virtual void DeleteDatabase()
    {
        _objectContext.DeleteDatabase();
    }

    public virtual void DeleteObject(object entity)
    {
        _objectContext.DeleteObject(entity);
    }

    public virtual void Detach(object entity)
    {
        _objectContext.Detach(entity);
    }

    public virtual void DetectChanges()
    {
        _objectContext.DetectChanges();
    }

    public virtual void Dispose()
    {
        _objectContext.Dispose();
    }

    public virtual int ExecuteFunction(string functionName, params ObjectParameter[] parameters)
    {
        return _objectContext.ExecuteFunction(functionName, parameters);
    }

    public virtual ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, params ObjectParameter[] parameters)
    {
        return _objectContext.ExecuteFunction<TElement>(functionName, parameters);
    }

    public virtual ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, MergeOption mergeOption, params ObjectParameter[] parameters)
    {
        return _objectContext.ExecuteFunction<TElement>(functionName, mergeOption, parameters);
    }

    public virtual int ExecuteStoreCommand(string commandText, params object[] parameters)
    {
        return _objectContext.ExecuteStoreCommand(commandText, parameters);
    }

    public virtual ObjectResult<TElement> ExecuteStoreQuery<TElement>(string commandText, params object[] parameters)
    {
        return _objectContext.ExecuteStoreQuery<TElement>(commandText, parameters);
    }

    public virtual ObjectResult<TEntity> ExecuteStoreQuery<TEntity>(string commandText, string entitySetName, MergeOption mergeOption, params object[] parameters)
    {
        return _objectContext.ExecuteStoreQuery<TEntity>(commandText, entitySetName, mergeOption, parameters);
    }

    public virtual object GetObjectByKey(EntityKey key)
    {
        return _objectContext.GetObjectByKey(key);
    }

    public virtual void LoadProperty(object entity, string navigationProperty)
    {
        _objectContext.LoadProperty(entity, navigationProperty);
    }

    public virtual void LoadProperty(object entity, string navigationProperty, MergeOption mergeOption)
    {
        _objectContext.LoadProperty(entity, navigationProperty, mergeOption);
    }

    public virtual void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector)
    {
        _objectContext.LoadProperty<TEntity>(entity, selector);
    }

    public virtual void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector, MergeOption mergeOption)
    {
        _objectContext.LoadProperty<TEntity>(entity, selector, mergeOption);
    }

    public virtual System.Data.Metadata.Edm.MetadataWorkspace MetadataWorkspace
    {
        get { return _objectContext.MetadataWorkspace; }
    }

    public virtual ObjectStateManager ObjectStateManager
    {
        get { return _objectContext.ObjectStateManager; }
    }

    public virtual void Refresh(RefreshMode refreshMode, IEnumerable collection)
    {
        _objectContext.Refresh(refreshMode, collection);
    }

    public virtual void Refresh(RefreshMode refreshMode, object entity)
    {
        _objectContext.Refresh(refreshMode, entity);
    }

    public virtual int SaveChanges()
    {
        return _objectContext.SaveChanges();
    }

    public virtual int SaveChanges(bool acceptChangesDuringSave)
    {
        return _objectContext.SaveChanges(acceptChangesDuringSave);
    }

    public virtual int SaveChanges(SaveOptions options)
    {
        return _objectContext.SaveChanges(options);
    }

    public virtual ObjectResult<TElement> Translate<TElement>(DbDataReader reader)
    {
        return _objectContext.Translate<TElement>(reader);
    }

    public virtual ObjectResult<TEntity> Translate<TEntity>(DbDataReader reader, string entitySetName, MergeOption mergeOption)
    {
        return _objectContext.Translate<TEntity>(reader, entitySetName, mergeOption);
    }

    public virtual bool TryGetObjectByKey(EntityKey key, out object value)
    {
        return _objectContext.TryGetObjectByKey(key, out value);
    }
}
<#+
}
#>

WriteMockContextBody() (注释后)的前 2 行(第二行是大括号)更改为

<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#>Mock : ObjectContextMockBase, I<#=code.Escape(container)#>
{
    public <#=code.Escape(container)#>Mock(IObjectContext objectContext)
        : base(objectContext)
    {
    }

Change the 第一行 WriteInterface() (注释后) )

<#=Accessibility.ForType(container)#> interface I<#=code.Escape(container)#> : IObjectContext

我还应该注意,我还没有对此进行太多测试,但我确实做了一些基本验证以确保其功能。

I discovered this great tool for use with EF 4 for mocking the database. Basically it is just a T4 template that creates both the "normal" EF layer and also creates a mock object and interface that can be used to test. More documentation here.

One caveat, though. Out of the box it didn't work for me because my repository code calls SaveChanges() and other methods which are not implemented in the generated interface. I was able to get around this by extracting the interface from Microsoft's ObjectContext implementation and creating my own IObjectContext interface. I then created a base class (for the mock) that implements this interface by simply delegating each of the calls to an injected mock. This way I can use Moq to create that piece in my test class, while still retaining the ability to track inserts, updates, and deletes in the generated mock.

Here are my changes to the "Context" T4 template to fix this issue. Sorry for doing this in little bits and pieces - SO didn't let me post the entire code listing because it was too long.

Add this code before fileManager.Process();

fileManager.StartNewFile( "IObjectContext.cs");
WriteHeader();
WriteHeaderIncludeSystem();
WriteHeaderIncludeData();
WriteHeaderIncludeContainers();
WriteNamespaceBegin( code, namespaceName );
WriteObjectContextInterface( container, code );
WriteNamespaceEnd( namespaceName );

fileManager.StartNewFile( container.Name + "Mock.ObjectContext.cs");
WriteHeader();
WriteHeaderIncludeSystem();
WriteHeaderIncludeData();
WriteHeaderIncludeContainers();
WriteNamespaceBegin( code, namespaceName );
WriteObjectContextMockBase( container, code );
WriteNamespaceEnd( namespaceName );

Add this code after the WriteInterface() block.

<#+
void WriteObjectContextInterface( EntityContainer container, CodeGenerationTools code )
{
#>
/// <summary>
/// The interface for the generic object context. This contains all of
/// the <code>ObjectContext</code> properties that are implemented in the 
/// concrete ObjectContext class. This interface was created so these members
/// can be mocked, as ObjectContext doesn't have a default public constructor.
/// </summary>
<#=Accessibility.ForType(container)#> interface IObjectContext : IDisposable
{
    void AcceptAllChanges();
    void AddObject(string entitySetName, object entity);
    TEntity ApplyCurrentValues<TEntity>(string entitySetName, TEntity currentEntity) where TEntity : class;
    TEntity ApplyOriginalValues<TEntity>(string entitySetName, TEntity originalEntity) where TEntity : class;
    void ApplyPropertyChanges(string entitySetName, object changed);
    void Attach(System.Data.Objects.DataClasses.IEntityWithKey entity);
    void AttachTo(string entitySetName, object entity);
    int? CommandTimeout { get; set; }
    DbConnection Connection { get; }
    ObjectContextOptions ContextOptions { get; }
    void CreateDatabase();
    string CreateDatabaseScript();
    EntityKey CreateEntityKey(string entitySetName, object entity);
    T CreateObject<T>() where T : class;
    ObjectSet<TEntity> CreateObjectSet<TEntity>() where TEntity : class;
    ObjectSet<TEntity> CreateObjectSet<TEntity>(string entitySetName) where TEntity : class;
    void CreateProxyTypes(IEnumerable<Type> types);
    ObjectQuery<T> CreateQuery<T>(string queryString, params ObjectParameter[] parameters);
    bool DatabaseExists();
    string DefaultContainerName { get; set; }
    void DeleteDatabase();
    void DeleteObject(object entity);
    void Detach(object entity);
    void DetectChanges();
    void Dispose();
    int ExecuteFunction(string functionName, params ObjectParameter[] parameters);
    ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, params ObjectParameter[] parameters);
    ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, MergeOption mergeOption, params ObjectParameter[] parameters);
    int ExecuteStoreCommand(string commandText, params object[] parameters);
    ObjectResult<TElement> ExecuteStoreQuery<TElement>(string commandText, params object[] parameters);
    ObjectResult<TEntity> ExecuteStoreQuery<TEntity>(string commandText, string entitySetName, MergeOption mergeOption, params object[] parameters);
    object GetObjectByKey(System.Data.EntityKey key);
    void LoadProperty(object entity, string navigationProperty);
    void LoadProperty(object entity, string navigationProperty, MergeOption mergeOption);
    void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector);
    void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector, MergeOption mergeOption);
    System.Data.Metadata.Edm.MetadataWorkspace MetadataWorkspace { get; }
    ObjectStateManager ObjectStateManager { get; }
    void Refresh(RefreshMode refreshMode, IEnumerable collection);
    void Refresh(RefreshMode refreshMode, object entity);
    int SaveChanges();
    int SaveChanges(bool acceptChangesDuringSave);
    int SaveChanges(SaveOptions options);
    ObjectResult<TElement> Translate<TElement>(DbDataReader reader);
    ObjectResult<TEntity> Translate<TEntity>(DbDataReader reader, string entitySetName, MergeOption mergeOption);
    bool TryGetObjectByKey(EntityKey key, out object value);
}
<#+
}
#>

<#+
void WriteObjectContextMockBase( EntityContainer container, CodeGenerationTools code )
{
#>
/// <summary>
/// The default concrete implementation of IObjectContext that will be used for mocking. 
/// This contains all of the <code>IObjectContext</code> members that are implemented in the 
/// concrete ObjectContext class. This class was created so these members
/// can be mocked.
/// </summary>
<#=Accessibility.ForType(container)#> abstract class ObjectContextMockBase : IObjectContext
{
    private readonly IObjectContext _objectContext;
    public ObjectContextMockBase(IObjectContext objectContext)
    {
        if (objectContext == null)
            throw new System.ArgumentNullException("objectContext");
        _objectContext = objectContext;
    }

    public virtual void AcceptAllChanges()
    {
        _objectContext.AcceptAllChanges();
    }

    public virtual void AddObject(string entitySetName, object entity)
    {
        _objectContext.AddObject(entitySetName, entity);
    }

    public virtual TEntity ApplyCurrentValues<TEntity>(string entitySetName, TEntity currentEntity) 
        where TEntity : class
    {
        return _objectContext.ApplyCurrentValues<TEntity>(entitySetName, currentEntity);
    }

    public virtual TEntity ApplyOriginalValues<TEntity>(string entitySetName, TEntity originalEntity) 
        where TEntity : class
    {
        return ApplyOriginalValues<TEntity>(entitySetName, originalEntity);
    }

    public virtual void ApplyPropertyChanges(string entitySetName, object changed)
    {
        _objectContext.ApplyPropertyChanges(entitySetName, changed);
    }

    public virtual void Attach(System.Data.Objects.DataClasses.IEntityWithKey entity)
    {
        _objectContext.Attach(entity);
    }

    public virtual void AttachTo(string entitySetName, object entity)
    {
        _objectContext.AttachTo(entitySetName, entity);
    }

    public virtual int? CommandTimeout
    {
        get { return _objectContext.CommandTimeout; }
        set { _objectContext.CommandTimeout = value; }
    }

    public virtual DbConnection Connection 
    { 
        get { return _objectContext.Connection; }
    }

    public virtual ObjectContextOptions ContextOptions
    { 
        get { return _objectContext.ContextOptions; }
    }

    public virtual void CreateDatabase()
    {
        _objectContext.CreateDatabase();
    }

    public virtual string CreateDatabaseScript()
    {
        return _objectContext.CreateDatabaseScript();
    }

    public virtual EntityKey CreateEntityKey(string entitySetName, object entity)
    {
        return _objectContext.CreateEntityKey(entitySetName, entity);
    }

    public virtual T CreateObject<T>() 
        where T : class
    {
        return _objectContext.CreateObject<T>();
    }

    public virtual ObjectSet<TEntity> CreateObjectSet<TEntity>()
        where TEntity : class
    {
        return _objectContext.CreateObjectSet<TEntity>();
    }

    public virtual ObjectSet<TEntity> CreateObjectSet<TEntity>(string entitySetName) 
        where TEntity : class
    {
        return _objectContext.CreateObjectSet<TEntity>(entitySetName);
    }

    public virtual void CreateProxyTypes(IEnumerable<Type> types)
    {
        _objectContext.CreateProxyTypes(types);
    }

    public virtual ObjectQuery<T> CreateQuery<T>(string queryString, params ObjectParameter[] parameters)
    {
        return _objectContext.CreateQuery<T>(queryString, parameters);
    }

    public virtual bool DatabaseExists()
    {
        return _objectContext.DatabaseExists();
    }

    public virtual string DefaultContainerName
    {
        get { return _objectContext.DefaultContainerName; }
        set { _objectContext.DefaultContainerName = value; }
    }

    public virtual void DeleteDatabase()
    {
        _objectContext.DeleteDatabase();
    }

    public virtual void DeleteObject(object entity)
    {
        _objectContext.DeleteObject(entity);
    }

    public virtual void Detach(object entity)
    {
        _objectContext.Detach(entity);
    }

    public virtual void DetectChanges()
    {
        _objectContext.DetectChanges();
    }

    public virtual void Dispose()
    {
        _objectContext.Dispose();
    }

    public virtual int ExecuteFunction(string functionName, params ObjectParameter[] parameters)
    {
        return _objectContext.ExecuteFunction(functionName, parameters);
    }

    public virtual ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, params ObjectParameter[] parameters)
    {
        return _objectContext.ExecuteFunction<TElement>(functionName, parameters);
    }

    public virtual ObjectResult<TElement> ExecuteFunction<TElement>(string functionName, MergeOption mergeOption, params ObjectParameter[] parameters)
    {
        return _objectContext.ExecuteFunction<TElement>(functionName, mergeOption, parameters);
    }

    public virtual int ExecuteStoreCommand(string commandText, params object[] parameters)
    {
        return _objectContext.ExecuteStoreCommand(commandText, parameters);
    }

    public virtual ObjectResult<TElement> ExecuteStoreQuery<TElement>(string commandText, params object[] parameters)
    {
        return _objectContext.ExecuteStoreQuery<TElement>(commandText, parameters);
    }

    public virtual ObjectResult<TEntity> ExecuteStoreQuery<TEntity>(string commandText, string entitySetName, MergeOption mergeOption, params object[] parameters)
    {
        return _objectContext.ExecuteStoreQuery<TEntity>(commandText, entitySetName, mergeOption, parameters);
    }

    public virtual object GetObjectByKey(EntityKey key)
    {
        return _objectContext.GetObjectByKey(key);
    }

    public virtual void LoadProperty(object entity, string navigationProperty)
    {
        _objectContext.LoadProperty(entity, navigationProperty);
    }

    public virtual void LoadProperty(object entity, string navigationProperty, MergeOption mergeOption)
    {
        _objectContext.LoadProperty(entity, navigationProperty, mergeOption);
    }

    public virtual void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector)
    {
        _objectContext.LoadProperty<TEntity>(entity, selector);
    }

    public virtual void LoadProperty<TEntity>(TEntity entity, Expression<Func<TEntity, object>> selector, MergeOption mergeOption)
    {
        _objectContext.LoadProperty<TEntity>(entity, selector, mergeOption);
    }

    public virtual System.Data.Metadata.Edm.MetadataWorkspace MetadataWorkspace
    {
        get { return _objectContext.MetadataWorkspace; }
    }

    public virtual ObjectStateManager ObjectStateManager
    {
        get { return _objectContext.ObjectStateManager; }
    }

    public virtual void Refresh(RefreshMode refreshMode, IEnumerable collection)
    {
        _objectContext.Refresh(refreshMode, collection);
    }

    public virtual void Refresh(RefreshMode refreshMode, object entity)
    {
        _objectContext.Refresh(refreshMode, entity);
    }

    public virtual int SaveChanges()
    {
        return _objectContext.SaveChanges();
    }

    public virtual int SaveChanges(bool acceptChangesDuringSave)
    {
        return _objectContext.SaveChanges(acceptChangesDuringSave);
    }

    public virtual int SaveChanges(SaveOptions options)
    {
        return _objectContext.SaveChanges(options);
    }

    public virtual ObjectResult<TElement> Translate<TElement>(DbDataReader reader)
    {
        return _objectContext.Translate<TElement>(reader);
    }

    public virtual ObjectResult<TEntity> Translate<TEntity>(DbDataReader reader, string entitySetName, MergeOption mergeOption)
    {
        return _objectContext.Translate<TEntity>(reader, entitySetName, mergeOption);
    }

    public virtual bool TryGetObjectByKey(EntityKey key, out object value)
    {
        return _objectContext.TryGetObjectByKey(key, out value);
    }
}
<#+
}
#>

Change the first 2 lines (the 2nd one being the curly bracket) of WriteMockContextBody() (after the comment) to

<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#>Mock : ObjectContextMockBase, I<#=code.Escape(container)#>
{
    public <#=code.Escape(container)#>Mock(IObjectContext objectContext)
        : base(objectContext)
    {
    }

Change the first line of WriteInterface() (after the comment) to

<#=Accessibility.ForType(container)#> interface I<#=code.Escape(container)#> : IObjectContext

I should also note that I haven't done much testing with this yet, but I did do some basic verifications to ensure it functions.

暖伴 2024-11-15 23:21:13

这是帮助生成测试数据的库 - http://autopoco.codeplex.com/

Here is library that helps in generating test data - http://autopoco.codeplex.com/

画骨成沙 2024-11-15 23:21:13

请参阅 Rab Hallett 的博客,标题为 ADO.NET 模拟上下文生成器:视觉Studio 2010 模板

这里 Rab 使用 T4 模板来创建 EF 的界面。

See Rab Hallett's blog titled ADO.NET Mocking Context Generator: Visual Studio 2010 Template

Here Rab uses a T4 template to create the interface for EF.

自由范儿 2024-11-15 23:21:13

您可以查看此线程 关于面向数据库专业人员的 Visual Studio。或者 RedGate 有一个类似的工具。我不知道有什么免费工具可以做到这一点。

You can check out this thread about Visual Studio for Database Professionals. Or RedGate has a similar tool. I don't know of any free tools that would do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文