仅当实现实体DbEntityEntry.State与"Unchanged"不同时,才会调用IValidatableObject.Validate.只是更改导航属性不会改变状态,因此永远不会发生验证.
为什么微软总是发布半生不熟的测试版?
我甚至无法手动检测导航属性的变化:
var changes = context.ChangeTracker.Entries()
.Where(e => e.State != EntityState.Unchanged)
.ToArray();
Run Code Online (Sandbox Code Playgroud)
返回一个空数组.
我正在使用一个带有ASP.NET MVC 3应用程序的EF Code First方法,而不是重新创建轮子,我想知道是否已经存在一个可靠的存储库类,我的自定义存储库类可以扩展以便提供开箱即用的默认功能(例如基本CRUD等).
所以像这样......
public class CustomerRepository : BaseRepository { ... }
Run Code Online (Sandbox Code Playgroud)
...因此将提供一种默认的方式来开箱即用的Customer对象.我想在我的MVC控制器中注入一个ICustomerRepository,并在那里提供我的功能.
我确信这样的东西已经存在,因为我已经用NHibernate做了类似的事情.
谢谢
entity-framework dependency-injection ef-code-first asp.net-mvc-3 entity-framework-4.3
是否可以在Where()中对两个不同的属性进行查询?例如,我想获取所有在FirstName和LastName属性中包含"Robert G"的用户.如果我做 :
var contacts = _session.All<Contact>()
.Where(x => x.IsActive
&& (x.FirstName.ToLower().Contains(q.ToLower())
|| x.LastName.ToLower().Contains(q.ToLower())));
Run Code Online (Sandbox Code Playgroud)
我将不会得到"Robert G"的结果,因为FirstName包含"Robert"和LastName"Gambonni".
我还考虑过制作一个新属性FullName,它只是一个Getter,但是我必须先加载它们,因为我的属性不在数据库中.
有什么建议?非常感谢!
我收到错误,The property 'Rank' is not a declared property on type 'MasterUser'. Verify that the property has not been explicitly excluded from the model by using the Ignore method or NotMappedAttribute data annotation. Make sure that it is a valid primitive property.我检查了我的模型类,它就在那里.
public class MasterUser
{
//many other properties...
public virtual char Rank { get; set; }
//more properties below
}
Run Code Online (Sandbox Code Playgroud)
我还检查了我的数据库上下文,它也在那里并完美映射.
public class BBDataContext : DbContext
{
public DbSet<MasterUser> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用实体框架代码第一种方法.我在包管理器控制台中使用enable-migrations语句创建了模型并启用了迁移.我的上下文类包含映射配置和关系
modelBuilder.Configurations.Add(new PostMap());
modelBuilder.Configurations.Add(new UserMap());
Run Code Online (Sandbox Code Playgroud)
我的迁移文件中的Up方法201302261054023_InitialCreate.cs包含所有表定义,作为通过DbSet <Type>属性指定的上下文.
我在自定义数据库初始化程序中更改了InitializeDatabase方法,因为我想在数据库初始化期间迁移到特定的迁移.
public void InitializeDatabase(T context)
{
bool databaseExists = context.Database.Exists();
if (!databaseExists)
context.Database.CreateIfNotExists();
var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update("InitialCreate");
}
Run Code Online (Sandbox Code Playgroud)
可以修改201302261054023_InitialCreate类中的Up方法来添加另一个索引
CreateTable(
"dbo.City",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 450),
})
.PrimaryKey(t => t.Id);
Run Code Online (Sandbox Code Playgroud)
至
CreateTable(
"dbo.City",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(maxLength: 450),
})
.PrimaryKey(t => t.Id)
.Index(t=>t.Name,true);
Run Code Online (Sandbox Code Playgroud)
没有附加另一个迁移?
entity-framework ef-code-first ef-migrations entity-framework-5
我正在创建一个应用程序,我想尝试使用GUID.我首先使用实体框架代码和LINQ,所以我认为最简单的方法是使用
string guid = Guid.NewGuid().ToString();
var acc = new v_Account();
acc.v_AcctGuid = guid;
Run Code Online (Sandbox Code Playgroud)
然后将我的GUID存储为数据库中的类型字符串.这是一种不好的做法吗?GUID真的只是一个全局唯一的随机生成的字符串吗?
我很确定它会起作用,但我不想在未来遇到问题或通过这样做造成漏洞.
我有一个简单的课程:
public class TestClass<T> {
public int ID { get; set; }
public T Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用EF Code First和Repository模式将这种元素保存到SQL数据库中?
编辑:似乎无法使用-notation创建DbSet.例如
public class UnitOFWorkTestContext : DbContext {
...
public DbSet<TestClass<int>> TestClass_Int { get; set; }
...
}
Run Code Online (Sandbox Code Playgroud)
给出错误:
未映射类型'UnitOFWorkTest.Models.TestClass`1 [System.Int32]'.使用Ignore方法或NotMappedAttribute数据批注检查未明确排除类型.验证类型是否已定义为类,不是原始类,嵌套类还是通用类,并且不从EntityObject继承.
我已经搜索了大量关于有界上下文的内容,我知道它是域驱动设计中的一种模式,它是使用数据库上下文将我们的大型模型划分为更小的模型,但这让我有点混乱.事实上我不知道它到底是做什么的?使用这种模式有什么好处
请帮助我理解这种模式.
domain-driven-design entity-framework ef-code-first bounded-contexts
我可能不是SQL专家,但在进行性能测试时,我看到EF6(Code-First)生成以下语句:
SELECT
[UnionAll3].[C2] AS [C1],
[UnionAll3].[C3] AS [C2],
[UnionAll3].[C4] AS [C3],
[UnionAll3].[C5] AS [C4],
[UnionAll3].[C6] AS [C5],
[UnionAll3].[C7] AS [C6],
[UnionAll3].[C8] AS [C7],
[UnionAll3].[C1] AS [C8],
[UnionAll3].[C9] AS [C9],
[UnionAll3].[C10] AS [C10],
[UnionAll3].[C11] AS [C11],
[UnionAll3].[C12] AS [C12],
[UnionAll3].[C13] AS [C13],
[UnionAll3].[C14] AS [C14],
[UnionAll3].[C15] AS [C15],
[UnionAll3].[C16] AS [C16],
[UnionAll3].[C17] AS [C17],
[UnionAll3].[C18] AS [C18],
[UnionAll3].[C19] AS [C19],
[UnionAll3].[C20] AS [C20],
[UnionAll3].[C21] AS [C21],
[UnionAll3].[C22] AS [C22],
[UnionAll3].[C23] AS [C23],
[UnionAll3].[C24] AS [C24],
[UnionAll3].[C25] AS [C25],
[UnionAll3].[C26] AS [C26], …Run Code Online (Sandbox Code Playgroud) 我在使用GetType()和typeof()获得类的类型时遇到问题,问题在于它无法正常工作。
我有Content的基类和Podcast和AudioBook的继承类。
我使用的是Code First,并且每个层次结构都有一个表(该表将所有子类存储在一个带有Discriminator列的表中)来存储所有Content实体。
我想通过“标题”列查询“内容”表,并返回一个“内容”实体。然后,根据类型(Podcast,AudioBook)执行其他操作。但是类型检查不起作用。
楷模
public abstract class Content
{
public string Title { get; set; }
}
public class Podcast : Content
{
}
Run Code Online (Sandbox Code Playgroud)
资料库
public Content FindContentByRoutingTitle(string routingTitle)
{
var content = Context.ContentItems
.FirstOrDefault(x => x.RoutingTitle == routingTitle);
return content;
}
Run Code Online (Sandbox Code Playgroud)
控制者
var content = _contentRepository.FindContentByRoutingTitle(title);
if (content.GetType() == typeof(Podcast))
{
return RedirectToAction("Index", "Podcast", new { title = title });
}
else if (content.GetType() == typeof(Content))
{
//just a check to see if equating with …Run Code Online (Sandbox Code Playgroud)