如何编写验证实体框架数据库第一模型的测试 [英] How do I write a test to verify Entity Framework Database First Model

查看:186
本文介绍了如何编写验证实体框架数据库第一模型的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体框架数据库第一模型。



我想编写一个MSTest / nUnit测试来验证在我的edmx模型中定义的所有存储过程和表在数据库上仍然有效。



有一个大型开发人员,有些只是在c#中处理存储过程和其他,我想运行一个集成测试来验证/验证项目中的EF模型。 >

我在Linq2Sql中进行了一次测试,以查找存储过程调用中常见的属性,然后运行SQL查询以验证存储过程是否仍然存在。
我现在已升级到EF6,我想在构建中保持类似的理智检查。



这是我到目前为止。

  var list = context.MetadataWorkspace.GetItems&EntityType>(DataSpace.CSpace); 

var badSp = new List< string>();

foreach(列表中的var表)
{
if(!DoesTableExist(dbContext,table))
{
badSp.Add(table.Name );
}
}

if(badSp.Any())
{
var retval = new StringBuilder();
retval.AppendLine(以下对象不存在于数据库中,但不存在+ dbContext.GetType()。Name +.edmx,它们可能已过时);
badSp.Sort();
foreach(var sp in badSp)
{
retval.AppendLine(sp);
}
Assert.Fail(retval.ToString())
}

我提出的一些问题是,这并不告诉我表格是否在不同的模式中。模式返回null。我有多个模式的表。



我也想做一个类似的测试来验证表和视图,他们也在不同的模式。

解决方案

我找到答案:

  var list = context.MetadataWorkspace.GetItems&EdimFunction>(DataSpace.SSpace).Where(i => i.ReturnParameter == null); 

这返回了正确的模式和存储过程名称。我可以调用列表中的每个项目:

  var sqlCommands = string.Format(SELECT'x'FROM sys。对象WHERE object_id = OBJECT_ID(N'{0}')AND type in(N'P',N'PC'),storedProcedureName); 
var exists = dbContext.Database.SqlQuery< string>(sqlCommands).Any();


I have a Entity Framework Database First Model.

I want to write a MSTest/nUnit test to verify that all the Stored procs and tables that are defined in my edmx model are still valid on the database.

With a large team of developers some only working on stored procedures and other in c# I would like to run an integration test to validate/verify the EF models in the project.

I had a test in Linq2Sql to look for an attribute that is common on the stored procedure calls then it ran a SQL Query to verify the stored procedure still exists. I've now upgraded to EF6 and I want to keep a similar sanity check in the build.

This is what I have so far.

 var list = context.MetadataWorkspace.GetItems<EntityType>(DataSpace.CSpace);

 var badSp = new List<string>();

 foreach (var table in list)
 {
     if (!DoesTableExist(dbContext, table))
     {
       badSp.Add(table.Name);
     }
 }

 if (badSp.Any())
 {
            var retval = new StringBuilder();
            retval.AppendLine("The Following Objects do not exist in the database but do not exist the " + dbContext.GetType().Name + ".edmx, they may be obsolete");
            badSp.Sort();
    foreach (var sp in badSp)
    {
       retval.AppendLine(sp);
    }
    Assert.Fail(retval.ToString())  
}

Some issues I've come up with is this doesn't tell me if a table is in a different schema. Schema is returning null. I have tables in multiple schemas.

I also want to do a similar test to verify tables and views, they're in different schemas also.

解决方案

I found the answer:

var list = context.MetadataWorkspace.GetItems<EdmFunction>(DataSpace.SSpace).Where(i=>i.ReturnParameter == null);

This returned the correct schema and stored procedure name. I then could call for each item in the list:

var sqlCommands = string.Format("SELECT 'x' FROM sys.objects WHERE object_id = OBJECT_ID(N'{0}') AND type in (N'P', N'PC')", storedProcedureName);
var exists = dbContext.Database.SqlQuery<string>(sqlCommands).Any();

这篇关于如何编写验证实体框架数据库第一模型的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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