我正在使用EF5和Database-First模型.和Visual Visual Studio中的数据库项目一起维护应用程序的Sql Server数据库模式.
要更新EF模型,我将在空数据库中部署更改...
是否可以从Visual Studio(2012)数据库项目生成和更新EF模型?
更新:
从dacpac文件生成它是一个不错的选择.可能吗?
更新: 在MS Build 2014大会上,ADO.NET团队建议EF的未来版本(如EF7)仅适用于Code First方法.
后来,他们澄清了新方法的名称,不应该是Code First,尽管代码库建模.也许并不完全相同,但据我所知,它似乎与我非常相似.
所以我要尝试@ adam0101解决方案.任何其他以CodeFisrt结尾的提议解决方案都是从SSDT项目迁移到EF的CodeFisrt项目,我想要的是两者的平滑共存(也许我是一个梦想家......).
c# entity-framework visual-studio ef-database-first visual-studio-2012
我发现的所有关于声明默认值的方法都是在 Sql 脚本中生成默认值,而不是在迁移代码中。
我最喜欢的是使用属性:https : //stackoverflow.com/a/34894274/132942
[SqlDefaultValue(DefaultValue = "getutcdate()")]
public DateTime CreatedDateUtc { get; set; }
Run Code Online (Sandbox Code Playgroud)
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class SqlDefaultValueAttribute : Attribute
{
public string DefaultValue { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
modelBuilder.Conventions.Add( new AttributeToColumnAnnotationConvention<SqlDefaultValueAttribute, string>("SqlDefaultValue", (p, attributes) => attributes.Single().DefaultValue));
Run Code Online (Sandbox Code Playgroud)
然后自定义SqlGenerator。但我不喜欢这样,因为我不知道在生成迁移时是否一切都如我所愿。
为此,我像这样修改了MigrationCodeGenerator:https : //stackoverflow.com/a/21024108
public class ExtendedMigrationCodeGenerator : MigrationCodeGenerator
{
public override ScaffoldedMigration Generate(string migrationId, IEnumerable<MigrationOperation> operations, string sourceModel, string targetModel, string @namespace, string className)
{
foreach (MigrationOperation …Run Code Online (Sandbox Code Playgroud) entity-framework default-value ef-code-first entity-framework-6 entity-framework-migrations
我们有一个程序连接到我们的数据库并运行一些存储过程来获取一些数据。
数据库是 SQL Server 2008,程序在本地运行。连接是通过 TCP/IP 进行的,但已启用共享内存。并且连接字符串的超时设置为 45 秒。
当我们通过 SQL Server Management Studio 运行它们时,运行需要 0-5 秒。当我们通过代码运行它时,它会随机超时。
为了进行一些测试,我们将超时时间从 45 秒增加到几分钟。并且为了丢弃任何阻塞问题,我们检查了存储过程仅“选择”数据(没有插入或更新语句)。我们已经为 select 语句尝试了几个表修饰符,例如:nolock, readpast, ...
我也检查过sp_who2,dbcc opentran()没有任何东西被阻止.......Net 的 SPID 是running command .... 有关 .Net 在等待 DB 回答时的更多详细信息,通过 SQL Server Management Studio,我可以毫无问题地运行相同的语句(存储过程或选择)。
关于发生了什么的任何建议?
我是EF的新手,我从EF5开始.
遵循实体框架5的性能注意事项的建议,2.4.2将模型移动到单独的程序集...我创建了一个单独的项目(我将其称为EfPrj)来管理我的Db上下文(称为MyDbContext).
在我的域层(我将其称为DomainPrj)中,我使用了EfPrj中的实体.问题是在DomainPrj中我看不到继承自DbContext的MyDbContext成员.
例如,如果我想更新表用户,则代码为:
void UpdateUser(User u)
{
MyDbContext db = new MyDbContext();
//whatever is needed
db.SaveChanges();
}
Run Code Online (Sandbox Code Playgroud)
但是在EfPrj中它没有问题.在DomainPrj中,我看不到成员函数SaveChanges(),收到此错误:
'MyDbContext' does not contain a definition for 'SaveChanges' and no extension method 'SaveChanges' ... could be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
更新: EfPrj只是一个带有ORM数据库优先模型的项目.MyDbContext默认定义为EF5对象:public partial class MyDbContext:DbContext {public MyDbContext():base("name = MyDbEntities"){}
所以派生自DbContext并且是公开的.DomainPrj引用EfPrj并且MyDbContext db = new MyDbContext()没有问题.
我想要一个带有(例如)SortedList集合"SrtdLst"属性的类"A",并且在这个类"A"中允许添加或减去"SrtdLst"项.但是在类"A"的实例中,只允许获取或设置项目的内容,而不是添加新项目或减去现有项目.在代码中:
class A
{
public SortedList<string, string> SrtdLst = new SortedList<string, string>();
public A()
{
// This must work:
SrtdLst.Add("KeyA", "ValueA");
// This too:
SrtdLst["KeyA"] = "ValueAAA";
}
}
class B
{
public A a = new A();
public B()
{
// I want the following code to fail:
a.SrtdLst.Add("KeyB", "ValueB");
// But this must work:
a.SrtdLst["KeyA"] = "ValueBBB";
}
}
Run Code Online (Sandbox Code Playgroud)
更新:我想创建一个类System.Data.SqlClient.SqlCommand.对于存储过程,您可以使用填充"参数"集合的成员"DeriveParameters",因此只能修改每个项目的值.
如何才能做到这一点?
我有一组MyClass1, MyClass2, MyClass3实现接口(IMyClass)的classes ().
我有兴趣创建一个方法来填充和返回Collection.Generics.Lists<>具有公共接口MyClassX(List<IMyClass>)的对象的list ().但是怎么样?
每个类都有一个构造函数MyClassX,可以执行不同的操作.所以我无法创建BaseClass并为所有人使用通用构造函数.
按接口我做不到:
public List<IMyClass> Fill(string[] ItemsPassedByParam, IMyClass InstanceOfMyClassX)
List MyList = new List<IMyClass>();
foreach (item in ItemsPassedByParam)
{
MyList.Add (new IMyClass (item)); //can't do new from an Interface!!
}
}
Run Code Online (Sandbox Code Playgroud)
此外,我尝试在下一个示例中使用泛型类型TMyClass,但两个都没有为我工作(虽然我对泛型类型没有非常深刻的概念):
public List<TMyClass> Fill(string[] ItemsPassedByParam, Type TypeOfMyClassX)
List MyList = new List <TMyClass> ();
foreach (item in ItemsPassedByParam)
{
MyList.Add (new TMyClass (item)); //can't do new from a …Run Code Online (Sandbox Code Playgroud) c# ×5
collections ×2
.net ×1
entity-framework-migrations ×1
generics ×1
getter ×1
interface ×1
setter ×1
sql-server ×1
sqlclient ×1
timeout ×1
types ×1