注意:我使用的是Entity Framework第5版
在我的通用库,我有Add,Edit和Delete下面的方法:
public class EntityRepository<T> : IEntityRepository<T>
where T : class, IEntity, new() {
readonly DbContext _entitiesContext;
public EntityRepository(DbContext entitiesContext) {
if (entitiesContext == null) {
throw new ArgumentNullException("entitiesContext");
}
_entitiesContext = entitiesContext;
}
//...
public virtual void Add(T entity) {
DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
if (dbEntityEntry.State != EntityState.Detached) {
dbEntityEntry.State = EntityState.Added;
}
else {
_entitiesContext.Set<T>().Add(entity);
}
}
public virtual void Edit(T entity) {
DbEntityEntry dbEntityEntry = _entitiesContext.Entry<T>(entity);
if (dbEntityEntry.State == …Run Code Online (Sandbox Code Playgroud) 我可以使用包管理器在本地运行'update-database -verbose'.
可能是一个愚蠢的问题,但我无法在线找到它 - 一旦部署了我的网站 - 我怎样才能在服务器上手动运行?
其次 - 您建议将数据库迁移部署到生产中的其他策略是什么?它们将如何更好?
谢谢
asp.net-mvc entity-framework asp.net-mvc-4 ef-migrations entity-framework-5
在这个简单的例子中,我有两个实体:Event和Address.我每晚都运行一个控制台应用程序来从XML源导入事件数据并将其添加到我的数据库中.
当我遍历XML事件节点(在Entity Framework上下文中)时,我检查是否有数据库中已存在给定值的地址记录.如果没有,它会添加一条新记录.
using (DemoContext context = new DemoContext())
{
foreach (XmlNode eventNode in eventsXml.SelectNodes("/Events/Event"))
{
Event newEvent = new Event();
newEvent.Title = **get from XML**
Address address = context.Addresses.Where(a =>
a.Title.Equals(title, StringComparison.OrdinalIgnoreCase) &&
a.Address1.Equals(address1, StringComparison.OrdinalIgnoreCase) &&
a.Address2.Equals(address2, StringComparison.OrdinalIgnoreCase) &&
a.City.Equals(city, StringComparison.OrdinalIgnoreCase) &&
a.State.Equals(state, StringComparison.OrdinalIgnoreCase) &&
a.ZipCode.Equals(zipCode, StringComparison.OrdinalIgnoreCase)
).FirstOrDefault();
if (address != null)
newEvent.Location = address;
else
{
newEvent.Location.Title = title;
newEvent.Location.Address1 = address1;
newEvent.Location.Address2 = address2;
newEvent.Location.City = city;
newEvent.Location.State = state;
newEvent.Location.ZipCode = zipCode;
}
context.Events.Add(newEvent);
}
context.SaveChanges(); …Run Code Online (Sandbox Code Playgroud) c# entity-framework savechanges dbcontext entity-framework-5
我注意到,当我创建代码时,首先使用add-migration它进行数据库迁移会生成Down()方法和Up()方法.
如何告诉我的数据库降级?
如果我使用Database First,如何使我的上下文对象使用Entity Framework 5.0中的Enum功能.
使用DbGeography.Distance(otherLocation)距离的单位测量两个位置之间 的距离时?
甚至msdn信息和intellisense都无法指定距离的单位.
有谁知道?
[编辑]我正在使用srid 4326.大多数例子似乎都在使用.从我所能找到的,4326似乎是弧度,这让我完全无能为力.弧度用于测量角度/度数,那么这实际上意味着什么呢?
我该如何解决这些错误:
使用的工具:VS 2012,Entity Framework 5.0.0,T4MVCExtensions 3.5.0,.NET Framework 4.5
我一直在大型MVC解决方案中的项目中使用EF和T4模板而没有发生任何事故.现在,当我右键单击edmx图时,EF会响应以下消息:值不在预期范围内.当我编译EF项目时,它会回复这些消息:
数据库中的每个表都有一个主键.我很惊讶这个问题突然出现了.有没有人有一些建议如何解决这个问题?
提前致谢,
阿诺德
我正在使用ASP.NET MVC 4和Entity Framework 5.我有模型类和实体映射来将现有表映射到那些模型类.这一切都很好,工作得很好.
现在我想嘲笑这个.我创建了Unit Of Work,它接受DataContext并使用Generic Repository.在那之后,我构建了服务,以便能够立即从许多存储库获取数据,并且只需要有一个DataContext实例.这也很有效.
现在问题:我想用模拟数据测试服务.当我创建Unit Of Work实例时,我希望能够插入一个被模拟的DataContext而不是真正的DataContext.
我试图创建一个IContext接口,让真实的和模拟的DataContext实现,但遇到了DbSet的问题.我试图使用IDbSet并创建一个FakeDbSet但没有成功.我还在互联网上读到用IDbSet模拟上下文并使用FakeDbSet是一种糟糕的方法.
你知道最好的方法是什么?我现在拥有的是我想保留的行为,但是我真的希望能够在DataContext中模拟Model类中的数据.
我知道实体框架已经附带了工作单元行为,并且您不需要在其上添加额外的行为.但是我想把它包装在另一个跟踪所有存储库的类中(称为UnitOfWork类).
编辑:我写了两篇文章,用LINQ和Entity Framework解释我的解决方案.
http://gaui.is/how-to-mock-the-datacontext-linq/
http://gaui.is/how-to-mock-the-datacontext-entity-framework/
这是我的代码:
IRepository.cs
public interface IRepository<T> where T : class
{
void Add(T entity);
void Delete(T entity);
void Update(T entity);
T GetById(long Id);
IEnumerable<T> All();
IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
}
Run Code Online (Sandbox Code Playgroud)
IUnitOfWork.cs
public interface IUnitOfWork : IDisposable
{
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
void Save();
}
Run Code Online (Sandbox Code Playgroud)
Repository.cs
public class Repository<T> : IRepository<T> where T : class
{
private readonly IDbContext …Run Code Online (Sandbox Code Playgroud) 我首先使用Entity Framework 5,DB.我知道如何在我的模型上定义枚举,并将字段的类型设置为该枚举.
现在,我需要将字段映射MyField到外部定义的枚举,即不在EF模型(OtherNamespace.MyEnum)中.设计者不允许我将类型设置为模型之外的任何类型.我尝试手动编辑edmx文件,但这会导致错误:
错误10016:解析项目"MyField"时出错.异常消息是:'Unresolved reference'Othercomspace.MyEnum'.'.
OtherNamespace.MyEnum 由我的项目引用.
你怎么做呢?
我已经使用了Find(id)与实体框架5.但是集合扩展方法,很多的例子我看到的使用Where(s => s.Id == 1),而我加入FirstOrDefault()来获取对象,而不是一个集合.这是一种风格差异,还是有明显的.Where()偏好的功能原因?
.net linq linq-to-entities entity-framework entity-framework-5