- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在开始一个使用 Entity Framework 的新项目。我研究了有关如何创建数据库的选项,发现代码优先迁移最有意义(如果您需要知道原因,请参阅底部)。 Code-First 迁移让我可以使用任意 SQL,这意味着我仍然可以完全控制。在实践中,我发现问题在于,对于某些常见任务,使用 SQL 似乎是非常重复的。
出于我的目的,我不关心迁移中的扩展是否与提供者无关(我内联的 SQL 不是)。但是,我并没有真正在迁移框架中找到一个好的接缝或扩展点来添加这些东西。
举个具体的例子,假设我想为MS-SQL复制指定一个RowGuid列。每次出现的形式都是
Sql(
string.Format(
"Alter Table {0} Alter Column {1} Add ROWGUIDCOL",
table,
column ));
所以我写静态方法来摆脱一些冗余
Sql( MigrationHelper.SetRowGuid( table, column );
-或-
MigrationHelper.SetRowGuid(Sql, table, column); //passing the Sql method
可能可以在 DbMigration 上创建这些扩展方法中的任何一个,并通过 this.
访问它们。但这看起来仍然不合适:
CreateTable(
"dbo.CustomerDirectory",
c => new
{
Uid = c.Int(nullable: false),
CustomerUid = c.Int(nullable: false),
Description = c.String(nullable: false, maxLength: 50, unicode: false),
RowGuid = c.Guid(nullable: false),
})
.PrimaryKey(t => t.Uid)
.ForeignKey("dbo.Customer", t => t.CustomerUid);
this.SetRowGuid( Sql, "dbo.CustomerDirectory", "RowGuid" );
//Custom method here because of desired naming convention of Constraint
this.SetDefaultConstraint( Sql, "dbo.CustomerDirectory", "''" ):
这不是很糟糕,但对我来说仍然感觉像是一个 hack。我必须重复表名,并且需要确保生成的列名正确。我发现表名需要重复很多次,但列也是如此。然而,我真正想做的是添加到刚刚发生的表名和列名都已知的表声明中。
但是,我找不到一个好的扩展点来扩展流畅的界面或以其他方式以感觉一致的方式扩展代码优先迁移。我错过了什么吗?有没有人找到这样做的好方法?
关于我为什么处于这种情况的一些理由:
我不喜欢使用通用自定义属性解决方案来指示非映射数据库的通用解决方案,原因有几个,但最强烈的原因是它们不会被迁移自动拾取,这意味着额外的维护。模型优先解决方案被淘汰是因为它们不能完全控制数据库。 Database-First 由于控制而吸引人;但是,它没有 Code-First Migrations 提供的开箱即用的变更管理功能。因此,代码优先迁移似乎是赢家,因为 [代码优先] 模型驱动的更改是自动的,这意味着只有一件事需要维护。
最佳答案
我找到了一个解决方案,但我不确定它是否好。我不得不在兔子洞里走得比我想要的更远一点,它并不是真正的扩展点。
它允许我编写如下语句:
CreateTable(
"dbo.CustomerDirectory",
c => new
{
Uid = c.Int(nullable: false),
CustomerUid = c.Int(nullable: false),
Description = c.String(nullable: false, maxLength: 50, unicode: false),
RowGuid = c.Guid(nullable: false),
})
.PrimaryKey(t => t.Uid)
.ForeignKey("dbo.Customer", t => t.CustomerUid)
//SqlValue is a custom static helper class
.DefaultConstraint( t => t.Description, SqlValue.EmptyString)
//This is a convention in the project
//Equivalent to
// .DefaultConstraint( t => t.RowGuid, SqlValue.EmptyString)
// .RowGuid( t => t.RowGuid )
.StandardRowGuid()
//For one-offs
.Sql( tableName => string.Format( "ALTER TABLE {0} ...", tableName" );
我不喜欢:
我只是考虑在这里使用它,因为:
internal static class TableBuilderExtentions
{
internal static TableBuilder<TColumns> Sql<TColumns>(
this TableBuilder<TColumns> tableBuilder,
Func<string, string> sql,
bool suppressTransaction = false,
object anonymousArguments = null)
{
string sqlStatement = sql(tableBuilder.GetTableName());
DbMigration dbMigration = tableBuilder.GetDbMigration();
Action<string, bool, object> executeSql = dbMigration.GetSqlMethod();
executeSql(sqlStatement, suppressTransaction, anonymousArguments);
return tableBuilder;
}
[Pure]
private static DbMigration GetDbMigration<TColumns>(this TableBuilder<TColumns> tableBuilder)
{
var field = tableBuilder.GetType().GetField(
"_migration", BindingFlags.NonPublic | BindingFlags.Instance);
return (DbMigration)field.GetValue(tableBuilder);
}
/// <summary>
/// Caution: This implementation only works on single properties.
/// Also, coder may have specified the 'name' parameter which would make this invalid.
/// </summary>
private static string GetPropertyName<TColumns>(Expression<Func<TColumns, object>> someObject)
{
MemberExpression e = (MemberExpression)someObject.Body;
return e.Member.Name;
}
[Pure]
private static Action<string, bool, object> GetSqlMethod(this DbMigration migration)
{
MethodInfo methodInfo = typeof(DbMigration).GetMethod(
"Sql", BindingFlags.NonPublic | BindingFlags.Instance);
return (s, b, arg3) => methodInfo.Invoke(migration, new[] { s, b, arg3 });
}
[Pure]
private static string GetTableName<TColumns>(this TableBuilder<TColumns> tableBuilder)
{
var field = tableBuilder.GetType().GetField(
"_createTableOperation", BindingFlags.NonPublic | BindingFlags.Instance);
var createTableOperation = (CreateTableOperation)field.GetValue(tableBuilder);
return createTableOperation.Name;
}
}
关于c# - 有没有一种扩展代码优先迁移的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12418144/
我正在使用“laravel/lumen-framework”:“5.7.*” 我有两个中间件,第一个 AuthTokenAuthenticate 应该应用于所有路由,因此它在 bootstrap/ap
当同时播放两个音频时...声音会相互抵消。如何解决这个奇怪的现象? 我有一些代码,其中单击按钮时有音频,并且每隔十秒就有音频(在后台服务中)。我有以下代码来在十秒间隔播放时停止按钮音频,并且工作正常:
我有一个功能可以在我的网站上搜索用户, 我的网站上还有一个面向 friend 的功能。 我有一个查询要在我的网站上搜索正确的用户,并且 我有一个查询可以确定用户的 friend ,他们都按应有的方式工
是否可以对记录使用 GROUP BY? 例如,我有一大堆联系人数据,可能包含也可能不包含所有信息 - 在 CSV 意义上,如果可能看起来像这样: Test User, Address1, Addres
如何在客户端 JavaScript 中创建一个环境,其中与用户界面和 View 相关的任何代码优先于其他代码? 我知道你可以使用 setTimeout([function],0); 将事情推到下一个刻
Jasmine 有没有办法定义测试失败的概率? 例如,现在 500'ing 的服务比不显示在页面上的简单内容更糟糕。 谢谢! 最佳答案 这不是单元或集成测试的工作方式。以太测试是否失败。并且您的套件中
我正在为我参与的一个项目开发一个 API。该 API 将由 Android 应用、iOS 应用和桌面网站使用。几乎所有 API 都只有注册用户才能访问。该 API 允许通过 WSSE 进行身份验证,这
我正在开发一些库并创建了这个有缺陷的代码: //------------------- Gmaps = {}; Gmaps.map = new Gmaps4RailsGoogle(); //there
我有一个使用[NSLocale ISOCountryCodes]获得的国家/地区的NSArray。如何排序此NSArray,以便可以将某些常用国家(地区)放在列表的顶部,同时将其余国家/地区按字母顺序
我正在为注册表编写代码,因为我正在从另一个文件中为电话号码列导入代码,但是当我将该代码放入其中时,您可以看到@include('layouts.phone');它显示为 当我放置@include('l
我刚刚遇到了 javascript 代码 file_upload_started = progress < 100; 我不知道如何阅读它,谷歌也没有真正出现太多。我什至不知道该怎么调用它,所以很难进行
目前,我正在 cppinstitute.org 学习 C 语言认证类(class)。在其中一个测验中,有一个如下的问题来识别输出。 int i = 1,j= 1; int w1,w2; w1 = (i
我想将无符号短值从 MSB 优先转换为 LSB 优先。做了下面的代码,但它不工作。有人可以指出我所做的错误吗 #include using namespace std; int main() {
考虑以下场景:我的应用程序有一些依赖于我自己的 POM 优先 Artifact (使用纯 Maven 构建)和一些依赖于我自己的 list 优先 Artifact (使用 Tycho 构建)。对于 P
拥有它应该是很自然的事情,我想知道是否有来自 TPL DataFlow 库的优先级缓冲区块的现成实现? 最佳答案 似乎实现这一目标的最佳方法是使用专门的 任务调度器 ,而不是实现您自己的 Buffer
我有一个 date 字段,它显示为从今天开始的天数。因此 2055-01-01 和 1950-01-01 将分别显示为正数和负数。现在我希望对这些进行排序,以便非负数按升序排在第一位,然后负数按降序排
我遇到一个问题,我看到我的事件类和悬停类正在 Firebug 中应用,但它没有优先于现有样式。 因此,如果我的元素设置了背景颜色,则事件和悬停背景颜色不会更改元素。 我该如何解决这个问题? 最佳答案
我正在考虑为 Salesforce Outbound Messaging 实现监听器应用程序。 walk through 使用已弃用的 ASMX Web 服务实现它。代码是使用带有/serverInt
对于每个表,EF 都会生成一个部分类,其中所有字段都可以公开访问,例如 public int ID { get; set; } 是否可以将 set 设为私有(private)?然后,我将只允许调用我的
我正在为水电站编写一个数据评估应用程序。我需要从服务器下载数据,该数据就在那里 - 作为 MySQL 表,格式化为 JSON 数组。现在,经过无数个小时的工作,我已经完成了连接到服务器、下载数据并将其
我是一名优秀的程序员,十分优秀!