看起来CodeFirst在没有完全控制数据库时停止做功课(我想).该场景是一个托管在Arvixe.com上的网站(或者我认为任何其他共享托管服务器),我必须从他们的控制面板创建数据库(而不是使用Sql Server Management Studio,只是说......).一旦创建了一个空数据库,我在网站上注册了一个连接,我用它从poco对象生成数据库,如:
add-migration m1 -targetdatabase myconnection
这正确地生成了我的FIRST迁移,我可以毫无问题地应用
update-database -targetdatabase myconnection
第一个问题,不是太重要,因为数据库是现有的,它不会发出Seed命令,所以我必须手动插入我的第一个记录,但这不是一个大问题.
然后我改变了我的poco对象,我需要更新数据库,但是当我发出另一个时
add-migration m2 -targetdatabase myconnection
它给出了错误:
System.Data.Entity.Migrations.MigrationsPendingException:由于以下显式迁移未决,因此无法生成显式迁移:[201111081426466_m1].在尝试生成新的显式迁移之前应用挂起的显式迁移.
这真的很奇怪,因为如果我查看数据库,我甚至可以看到表__MigrationHistory,但它看起来似乎无法识别它...
任何有同样问题的人,或者调查哪些好的小费?在此先感谢Andrea Bioli
entity-framework code-first entity-framework-4 ef-code-first
我希望在模型中使用DateTime属性,并以MSSQL2008类型为后盾,该类型至少可以保持3毫秒的精度。我想先做模型,即可以在VS2010设计器中单击“从模型生成数据库...”,而无需进行进一步调整即可获得正确的结果(我知道有很多解决方法)。
我自己的研究:我已经看过有关datetime2问题的其他一些话题,但是问题似乎恰恰相反,当他们想要datetime时获取datetime2。编写的解决方案建议EDMX中的ProviderManifestToken =“ 2005”给出日期时间,而ProviderManifestToken =“ 2008”给出datetime2行为。
我想要datetime2,并且已经有了ProviderManifestToken =“ 2008”,但仍然只能获取datetime。有一个MSDN线程建议使用手动修改的T4-template。但是调整T4(或文本编辑器中的EDMX文件)是否是在Model First中完成datetime2行为的唯一方法?这不是向EF模型添加精确的datetime属性的好方法...
更新: 我已将此问题提交给Microsoft Connect,您可以在此处找到。如果您想增强它,请投票...
有没有一种很好的方法来检测ObjectContext实际提交更改的时间?
SavingChanges 在进入数据存储之前发生但我还需要一种方法来了解这些更改是否实际提交.
在此先感谢约翰
更新:
我所拥有的是代码第一个DbContext.这被提供给动态数据,我发现它使用DbContext的内部ObjectContext(在转换为IObjectContextAdapter时我可以访问它).不调用dbcontext的SaveChanges,而是使用objectcontext的SaveChanges.我想要做的就是在保存完成后收到通知(即事件SavedChanges),这样我就可以使缓存无效.
我读过关于POCO(普通老式CLR(或C#)对象)这里,和困惑的它的使用.
如果我可以使用Code First方法那么天气我需要创建.edmx文件?没有它,我如何使用poco与代码第一的方法?
如果我在这种情况下理解错误,请纠正我!
谢谢.
我在我的数据库中添加了一个函数,我希望它在我的代码中可以调用.对于存储过程,我右键单击SP并在模型浏览器中选择添加功能导入来实现此目的,但此选项不适用于我的功能.我该怎么做才能导入这个功能?
stored-procedures user-defined-functions entity-framework-4 database-first
我在使用EF5时遇到了此错误消息.想知道是否有人有这个答案.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Budget.Data
{
[Table("BudgetItems")]
public class BudgetItemRepository
{
[Column("MdaCode")]
public int MDACode { get; set; }
[Column("PersonalCost")]
public double PersonnelCost { get; set; }
[Column("OverheadCost")]
public double OverheadCost { get; set; }
[Column("RecurrentCost")]
public int RecurrentCost { get; set; }
[Column("CapitalCost")]
public double Capital { get; set; }
public double Allocation { get; set; }
[Column("BudgetYear")]
public String BudgetYear { get; set; }
[Column("RecordCreatedDate")]
public DateTime DateCreated …Run Code Online (Sandbox Code Playgroud) 我试图关闭身份以插入我自己的值,我遵循的步骤
StoredGeneratedPattern值更改None为标识列 StoredGeneratedPattern值更改为NoneEDMX文件尝试使用以下代码
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
int k = Context.ExecuteStoreCommand("SET IDENTITY_INSERT dbo.client ON");
Context.ClientInfoes.Add(testclient);
result = Context.SaveChanges();
int j = Context.ExecuteStoreCommand("SET IDENTITY_INSERT dbo.client OFF");
scope.Complete();
}
Run Code Online (Sandbox Code Playgroud)
但我仍然收到错误
当IDENTITY_INSERT设置为OFF时,无法在表中插入标识列的显式值
我错过了什么吗?还有其他选择吗?
c# identity entity-framework entity-framework-4 entity-framework-4.1
我试图从RedGate的这本免费电子书中了解Linq的一些性能影响 ftp://support.red-gate.com/ebooks/under-the-hood-of-net-memory-management-part1.pdf
在本书的第157-158页,他们创建了以下示例.
Order[] pastDueAccounts = null;
DateTimedueDate = DateTime.Today.AddDays(-7);
using(varcontext = new Context())
{
pastDueAccounts = context.Accounts.Where(account => account.DueDate < dueDate).ToArray();
}
Run Code Online (Sandbox Code Playgroud)
然后他们将lamda表达式的一部分重新考虑到以下函数中.
public bool PastDueAccount(Account account)
{
return account.DueDate < DateTime.Today.AddDays(-7);
}
Run Code Online (Sandbox Code Playgroud)
最后他们使用了这个功能如下.
Order[] pastDueAccounts = null;
using(varcontext = new Context())
{
pastDueAccounts = context.Accounts.Where(account => PastDueAccount(account)).ToArray();
}
Run Code Online (Sandbox Code Playgroud)
基于我到目前为止所研究的内容,它不可能运行这个linq查询,因为LINQ将无法识别该方法并且无法转换为存储表达式.我想知道这个例子是不对的,根本不可能运行,或者我是否只是很难听到如何模拟这个问题?
我为我的实体框架应用程序启用了迁移.现在,我想在远程服务器上更新我的数据库.我运行了这个命令:
PM> Update-Database -Script
Run Code Online (Sandbox Code Playgroud)
所以它生成了一个sql脚本.但是,这个脚本包含我在数据库中实际拥有的所有元数据,而不是所做的更改; 所以,当我尝试在我的远程服务器上运行这个sql脚本时,它表示表已经存在.
我怎样才能生成只包含必要更新的sql脚本?
sql migration asp.net-mvc entity-framework entity-framework-4
使用标准方法升级我的数据库需要7秒,这太慢了,因为我的数据库有3个表,每个表有5个字段,几乎没有数据.
var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
//Update database to latest migration
migrator.Update();
Run Code Online (Sandbox Code Playgroud)
使用Entity Framework 6.0.
每次启动我的应用程序时,此代码都会运行,这会影响我的性能.为什么这么慢?无论如何要加快速度?
这是我的app.config:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
Run Code Online (Sandbox Code Playgroud) c# sql-server-ce entity-framework-4 ef-code-first ef-migrations