我最近开始使用EF v4 Code Only库来处理我正在进行的一些项目.但是,我遇到了一些障碍.我似乎无法弄清楚连接字符串的正确格式.我使用以下代码来构建连接字符串:
string connectionString = new EntityConnectionStringBuilder
{
Provider = "System.Data.SqlClient",
ProviderConnectionString = new SqlConnectionStringBuilder
{
DataSource = "localhost",
InitialCatalog = "ASM_Testing",
IntegratedSecurity = true,
Pooling = false
}.ConnectionString
}.ConnectionString;
Run Code Online (Sandbox Code Playgroud)
但是,使用它会导致以下错误:
Specifications_for_EntityContext.When_logging_in_application_with_valid_app_role_and_password.Login_should_be_successful : System.ArgumentException : Some required information is missing from the connection string. The 'metadata' keyword is always required.
Stack Trace:
at System.Data.EntityClient.EntityConnection.ValidateValueForTheKeyword(DbConnectionOptions effectiveConnectionOptions, String keywordName)
at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
at System.Data.Objects.ObjectContext..ctor(String connectionString)
at ASM.Data.EntityContext..ctor(String connectionString) in C:\Users\Jon Rista\TFS\Advanced Service Management\Trunk\Main\Source\ASM.Data\EntityContext.cs:line 16
at Specifications_for_EntityContext.Behaves_like_EntityContext_connected_to_a_database.TestableEntityContext..ctor(String connectionString) in C:\Users\Jon Rista\TFS\Advanced …Run Code Online (Sandbox Code Playgroud) 我在MSDN上关注这篇文章.我把它移植到EF Code First.
public interface IUnitOfWork
{
IRepository<Employee> Employees { get; }
IRepository<TimeCard> TimeCards { get; }
void Commit();
}
Run Code Online (Sandbox Code Playgroud)
public class HrContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
public DbSet<TimeCard> TimeCards { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>()
.HasMany(e => e.TimeCards)
.WithOptional(tc => tc.Employee);
}
}
Run Code Online (Sandbox Code Playgroud)
public class SqlRepository<T> : IRepository<T>
where T : class
{
private readonly DbSet<T> entitySet;
public SqlRepository(DbContext context)
{
this.entitySet = context.Set<T>();
}
public …Run Code Online (Sandbox Code Playgroud) entity-framework code-first entity-framework-4 ef-code-first entity-framework-ctp5
我想修改一个用EF 4.1(Code First)开发的DB.我将项目升级到EF 4.3并按照以下步骤操作:http: //blogs.msdn.com/b/adonet/archive/2012/02/09/ef-4-3-automatic-migrations-walkthrough.aspx
一切进展顺利,但是当我想测试当前的数据库(EF 4.1 Code First)时,Update-Database会引发这个错误:
无法构建下一次迁移,因为目标数据库是使用早于EF 4.3的Code First版本创建的,并且不包含迁移历史记录表.要开始对此数据库使用迁移,请确保当前模型与目标数据库兼容并执行迁移更新过程.(在Visual Studio中,您可以使用Package Manager控制台中的Update-Database命令来执行迁移更新过程).
我想知道如何迁移EF 4.1(Code First)DB?此外,DB是实时的并且有数据,我不能删除表.
entity-framework code-first entity-framework-4.1 ef-migrations entity-framework-4.3
我目前正在使用EF4.3和Code First.创建我的对象(通过我的视图 - 只使用自动生成的创建),但是当我尝试编辑对象时,它不会保存任何与我的导航属性完全相关的更改.我一直在阅读关系,但我不明白如何告诉我的背景,这种关系已经发生了变化.
这是我的实现的一些示例代码.
@* Snippet from my view where I link into my ViewModel. *@
<div class="row">
<div class="editor-label">
@Html.LabelFor(model => model.ManagerID)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.ManagerID, ViewBag.Manager as SelectList, String.Empty)
@Html.ValidationMessageFor(model => model.ManagerID)
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是我的Controller实现(我的编辑的POST):
[HttpPost]
public ActionResult Edit(ProjectViewModel projectViewModel)
{
if (ModelState.IsValid)
{
Project project = new Project();
project.ProjectID = projectViewModel.ProjectID;
project.Name = projectViewModel.Name;
project.ProjectManager = repository.GetUser(projectViewModel.ManagerID);
repository.InsertOrUpdateProject(project);
repository.Save();
return RedirectToAction("Index");
}
ViewBag.Manager = new SelectList(repository.GetUsers(), "UserID", "FullName", projectViewModel.ManagerID);
return View(projectViewModel);
} …Run Code Online (Sandbox Code Playgroud) 我有以下课程.我有一个varDescription类的对象.我想var使用Linq to Sql或Lambda表达式选择与对象中提供的客户端相关的Balance .如何连接这些表以从帐户表中获取余额?
public class Description
{
public int DescriptionID { get; set; }
// Attributes
public int ClientID { get; set; }
[ForeignKey("ClientID")]
public virtual Client Client { get; set; }
}
public class Client
{
public int ClientID { get; set; }
// Attributes
public int UserID { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; }
}
public class User
{
public int UserID { get; set; }
// …Run Code Online (Sandbox Code Playgroud) 有两个实体,如贝娄:
public class Business
{
public int Id {get; set;}
public File Logo {get; set;}
public int? LogoId {get; set;}
public File Video {get; set;}
public int? Video {get; set;}
public ICollection<File> Images {get; set;}
}
public class File
{
// some file props, such as Id, Name, ...
}
Run Code Online (Sandbox Code Playgroud)
如何在业务删除时为文件配置级联删除?请考虑,我不需要任何导航从File到Business.
更新:
您可以在下面找到modelBuilder配置:
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<Entities.Business>()
.HasOptional(b => b.Logo)
.WithOptionalPrincipal()
.WillCascadeOnDelete();
modelBuilder.Entity<Entities.Business>()
.HasOptional(b => b.Video)
.WithOptionalPrincipal()
.WillCascadeOnDelete();
modelBuilder.Entity<Entities.Business>()
.HasMany(b => b.Images)
.WithOptional()
.WillCascadeOnDelete();
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
在表'Files'上引入FOREIGN KEY约束'FK_dbo.Files_dbo.Businesses_Business_Id1'可能会导致循环或多个级联路径.指定ON DELETE …
救命!- 我在导航模型ArticleType上收到以下错误:
发生了关系多重性约束违规
这是现有的数据库模式:

这是我的代码:
public class Article
{
public int ID { get; set; }
public virtual Stage Stage { get; set; }
public virtual ArticleType ArticleType { get; set; } // Causes the violation
}
public class ArticleType
{
public int ID { get; set; }
public string Title { get; set; }
}
public class Stage
{
public int ID { get; set; }
public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我使用流畅的api进行绘图,这是该协会的摘录
// This …Run Code Online (Sandbox Code Playgroud) 我正在为我的项目实现实体框架(v5)基于代码的迁移.
来自我们客户的数据库管理员有时会有点偏执,并希望手动执行sql脚本.
由于我们已经有很多版本并且我们支持SqlServer和Oracle并行,我们不希望从所有可能的版本管理多个更新脚本xxxx到yyyy是否有可能以与我调用它相同的方式以编程方式获取sql脚本在程序包管理器控制台中
Update-Database -Script
Run Code Online (Sandbox Code Playgroud)
因此,客户端将通过一个简单的控制台应用程序,取决于他的数据库系统和当前版本正确的sql脚本作为输出.
我正在开一个笑话网络应用程序.
我使用C#,实体框架,MVC,我做代码优先.
现在,当我尝试在我的数据库中读出某些内容时,我在标题中显示的错误就会显示出来.我不知道该寻找什么.我抬头看谷歌,但我没有得到答案......
我尝试提供您可能需要知道的所有代码:
public class Context : DbContext
{
public DbSet<Categorie> Categories {get;set;}
public Context() : base("Witze_DB")
{
}
}
public class HomeController : Controller
{
private Context db;
public HomeController(Context db)
{
this.db = db;
}
public ActionResult Index()
{
var allCats = db.Categories;
return View(allCats);
}
}
Run Code Online (Sandbox Code Playgroud)
这是Index.cshtml文件:
@model IEnumerable<Witze.Categorie>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model) …Run Code Online (Sandbox Code Playgroud) 我试图参数化动态查询并首先在Entity Framework代码中使用SqlQuery方法运行它.
我第一次执行SqlQuery它按预期工作,所以我确信查询或参数没有任何问题,但我立即执行相同的命令第二次相同的参数,我得到这个错误
"SqlParameter已经包含在另一个SqlParameterCollection中."
由于我已经ToList()在这里使用方法,我不知道原因是什么!
这是模拟代码.
using (var context = Common.GetDbContext())
{
var parameters = new List<SqlParameter>();
//populating parameters here...
var sqlQuery = "Select * from MyTable where UserId=@p1 and And Active=@p2";
// first time
var result = context.Database.SqlQuery<ResultType>(sqlQuery, parameters.ToArray()).ToList();
//second time
result = context.Database.SqlQuery<ResultType>(sqlQuery, parameters.ToArray()).ToList();
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?