我刚刚将我的MVC4项目升级到.NET 4.5和EF5并开始使用VS2012.在意识到我需要再次在程序包管理器中设置自动迁移后,我运行Enable-Migrations - EnableAutomaticMigrations并收到错误
No context type was found in the assembly 'MySolutionName'.
一些研究表示,它与EF5有关,不支持预发布.我跑了,Install-Package EntityFramework -IncludePrerelease但它说已经安装了EF5(当我之前通过NuGetmanager安装它时没有指定-IncludePrerelease.
有谁知道我必须做什么才能为我的项目启用迁移?
使用Entity Framework 5,MiniProfiler 2.为MiniProfiler.EF安装Nuget.
按如下方式创建连接
var conn = new EFProfiledDbConnection(DbConnections.GetSqlConnection(), MiniProfiler.Current);
return new MyDbContext(conn, true);
Run Code Online (Sandbox Code Playgroud)
尝试使用DbContext检索数据时,将返回以下错误:
Unable to determine the provider name for connection of type
'StackExchange.Profiling.Data.EFProfiledDbConnection'.`
Run Code Online (Sandbox Code Playgroud)
我尝试将以下内容添加到web.config:
<system.data>
<DbProviderFactories>
<remove invariant="StackExchange.Profiling.Data.ProfiledDbProviderFactory" />
<add name="StackExchange.Profiling.Data.ProfiledDbProviderFactory"
invariant="StackExchange.Profiling.Data.ProfiledDbProviderFactory"
description="StackExchange.Profiling.Data.ProfiledDbProviderFactory"
type="StackExchange.Profiling.Data.ProfiledDbProviderFactory, MiniProfiler, Version=2.0.2.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
</DbProviderFactories>
</system.data>
Run Code Online (Sandbox Code Playgroud)
这没有用.我也尝试过使用MiniProfiler.EntityFramework中不同的EFProviderFactories,但是无法使用它们.
如果我尝试运行MiniProfilerEF.Initialize();的App_Start,然后我在尝试访问数据库时,出现以下错误:
如果在Code First模式下使用,使用T4模板为Database First和Model First开发生成的代码可能无法正常工作.要继续使用Database First或Model First,请确保在执行应用程序的配置文件中指定了Entity Framework连接字符串.要使用从Database First或Model First生成的这些类,使用Code First添加任何其他配置,使用属性或DbModelBuilder API,然后删除引发此异常的代码.
卸下DbProviderFactories从web.config部分,并运行MiniProfilerEF.Initialize_EF42();在App_Start在原来的错误.
正如MiniProfiler页面所说的那样,MiniProfilerEF.Initialize()首先只是代码,这似乎不是一种可行的方式.
其他搜索没有给我任何其他的尝试,除了在Web.Config中添加部分.有关如何进行的任何建议?目标是能够使用由Mvc MiniProfiler分析的Model First DbContext.
entity-framework mvc-mini-profiler asp.net-mvc-4 entity-framework-5
首先在Entity Framework 5模型中,由于生成类文件的方式,似乎存在一些重大更改(不再生成代码,但是T4模板)
2个例子:
是否有更多的突破性变化?他们的解决方案是什么?
我的MS SQL数据库中有3个表,我已经在我导入这3个表的项目中添加了EntityFramework(最新).第一个问题是没有建立任何实体,所以我将"代码生成策略" None改为Default.
建成之后,我得到了
X已经包含Y的定义
在实体的所有属性上.
当仔细观察时,它会产生一个部分ex Users.cs和一个部分Userin MainModel.Designer.cs?
为什么会产生User.cs?我在另一个项目中有类似的设置,EF设置相同的设置,没有User.cs?
Edit1:我可以看到一个不同的东西,那就是在失败的项目中使用强空间类型设置为False,但是不能将它设置为true(灰色)?
我知道EF 5会自动缓存查询,但是它是按照上下文还是整体来做的?我们正在使用MVC并将调用包装在using块中以处理dbcontext.例如:
public class Employee
{
public string FirstName {get; set;}
public string LastName {get; set;}
public int ID {get; set;}
}
public class EmployeeQueryByFirstName : IQueryObject<Employee>
{
private string _firstName;
public void Set(string FirstName)
{
_firstName = FirstName;
}
public Expression<Func<Employee,bool>> AsExpression()
{
return (e=>e.FirstName == this._firstName);
}
}
public class RepoExcerpt
{
public TEntity Find<TEntity>(IQueryObject<TEntity> queryObject)
where TEntity : class
{
using (var conn = ServiceLocator.IOC.Resolve<IDbContext>())
{
var query = (from q in conn.Set<TEntity>()
select q);
query = …Run Code Online (Sandbox Code Playgroud) 我似乎无法在流畅的API中找到一对多的关系语法.
作为一个例子,我有两个表如下
用户
Id
Name
Run Code Online (Sandbox Code Playgroud)
UserHistory
Id
UserId
Date
Run Code Online (Sandbox Code Playgroud)
在课程中,我有以下内容
public class User
{
public int Id { get; set; }
public virtual ICollection<UserHistory> Histories { get; set; }
}
public class UserHistory
{
public int Id { get; set; }
public int UserId { get; set; }
public DateTime Date { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我尝试过以下但我不确定它是否真的正确.
modelBuilder.Entity<User>()
.HasRequired(w => w.Histories)
.WithMany();
modelBuilder.Entity<User>()
.HasMany(f => f.Histories)
.WithOptional()
.HasForeignKey(f => f.UserId);
Run Code Online (Sandbox Code Playgroud)
一对多关系的正确语法是什么?
从技术上讲,我可以通过添加新表将其分解为多对多,但我不想引入另一个表.
我的订单类有:
public int CustomerId { get; set; }
public Customer Customer { get; set; }
Run Code Online (Sandbox Code Playgroud)
我真的需要这两个属性来建立关系吗?
我没有使用断开连接的实体,我使用代码第一种方法.
c# entity-framework foreign-keys ef-code-first entity-framework-5
EntityFramework是否适用于Windows 8商店应用程序?
我正在使用Visual Studio 2012 Express for Windows 8.我开始怀疑,因为我无法使其工作.
我从NuGet安装了Entity Framework包我将System.Data.Entity和System.Data.Design添加到项目中
但无论我做什么,DbContext都不被认可......
有任何想法吗?
编辑:
EntityFramework不适用于Windows应用商店应用.我不明白微软在这方面的立场,他们很糟糕.
另一个解决方案是使用SQLite,但它不是ORM.那有什么意义呢?
c# .net-4.5 entity-framework-5 visual-studio-2012 windows-store-apps
TLDR; 如何使用Entity framework 5编码迁移添加全文索引
我在使用Entity框架迁移向数据库添加全文索引时遇到问题.它需要从一开始就在那里,所以我正在尝试修改自动生成的InitialCreate迁移以添加它.
由于没有办法通过DbMigrations API来实现,我使用了在'Up'代码末尾运行内联sql.
Sql("create fulltext catalog AppNameCatalog;");
Sql("create fulltext index on Document (Data type column Extension) key index [PK_dbo.Document] on AppNameCatalog;");
Run Code Online (Sandbox Code Playgroud)
当这个运行时,一切都被创建好,直到它到达这个sql,然后它抛出sql错误' CREATE FULLTEXT CATALOG语句不能在用户事务中使用.".这是预期的,并按设计工作.
值得庆幸的是,Sql()有一个重载,允许您在迁移事务之外运行sql.真棒!我想.
Sql("create fulltext catalog AppNameCatalog;", true);
Sql("create fulltext index on Document (Data type column Extension) key index [PK_dbo.Document] on AppNameCatalog;", true);
Run Code Online (Sandbox Code Playgroud)
但是低并且看到修改代码来执行此操作(参见上文)会导致新的超时错误' 超时已到期.操作完成之前经过的超时时间或服务器没有响应."
我已经尝试吐出sql并手动运行它,它工作正常.我也在生成的sql中使用和不在事务外运行它们并且它们是相同的,因此它必须是sql执行方式.
在此先感谢您的帮助!
我正在尝试将小数存储140.2705893427到SQL Server 2012表中.该列的数据类型为decimal(12, 10)但我收到错误:
{"Parameter value '140.2705893427' is out of range."}
Run Code Online (Sandbox Code Playgroud)
为什么是这样?