Tag: entity framework 6

将数据从控制器传递到视图布局的最佳做法是什么?

我目前有一个MVC网站,需要在每个页面的标题上都有动态内容。 我目前在控制器中正常获取所需数据并将其放在View Model中。 在视图中,我获取数据并将模板部分粘贴到Viewbag中,最后,在主布局页面上,我获取Viewbag数据并将其传递给控制标题的部分。 我已经读过,我不应该尽可能使用Viewbag,而且我传递数据的次数感觉不对。 我能想到改善这一点的唯一方法是在主布局上创建一个部分,然后将部分/数据放在视图的部分中 – 但是,有大约30页,这再次感觉不像正确的路线。 是否有更好的方法来执行此操作/获取必须转到主视图的动态数据的最佳实践是什么?

entity framework分析器 – 带有EF 6的ASP.NET MVC4 – 无法确定提供者名称

我已经在我的ASP.NET MVC4项目中使用EF 6一段时间了,没有任何问题。 当我向项目添加Entity Framework Profiler时出现问题,然后我突然收到以下错误:“无法确定类型为’System.Data.SqlClient.SqlClientFactory’的提供程序工厂的提供程序名称。确保ADO。 NET提供程序已在应用程序配置中安装或注册。“ 我尝试了一些建议,包括bin文件夹中不存在“EntityFramework.SqlServer.dll”的问题。 堆栈跟踪: System.Data.Entity.Config.DefaultInvariantNameResolver.GetService(Type type, Object key) +440 System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) +83 System.Data.Entity.Config.CachingDependencyResolver.GetService(Type type, Object key) +179 System.Linq.WhereSelectArrayIterator`2.MoveNext() +82 System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source, Func`2 predicate) +215 System.Linq.WhereSelectArrayIterator`2.MoveNext() +82 System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source, Func`2 predicate) +215 System.Data.Entity.Config.CompositeResolver`2.GetService(Type type, Object key) +71 System.Data.Entity.Config.IDbDependencyResolverExtensions.GetService(IDbDependencyResolver resolver, Object key) +84 System.Data.Entity.Internal.InternalConnection.get_ProviderName() +112 System.Data.Entity.Internal.DefaultModelCacheKeyFactory.Create(DbContext context) +120 System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +319 […]

entity framework – 一对一 – ReferentialConstraint映射到商店生成的列

我在EF中创建了一个简单的一对一关系。 但是当我尝试插入时,我收到以下错误: ReferentialConstraint映射到存储生成的列。 列:’ACCOUNT_ID’。 控制台应用示例: namespace EF_ConsoleApp_Test { public class Program { public static void Main(string[] args) { var account = new Account { AccountNumber = “00123456”, CustomerValue = new Customer { FirstName = “Joe” } }; using (var db = new MainContext()) { db.Accounts.Add(account); db.SaveChanges(); } } } [Serializable] [Table(“CUSTOMERS”)] public class Customer { […]

从IDbCommandInterceptor的实现中获取DbContext

我正在使用IDbCommandInterceptor实现: public class MyInterceptor : IDbCommandInterceptor { public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) { var context = interceptionContext.DbContexts.FirstOrDefault(); } public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext interceptionContext) { } public void ScalarExecuted(DbCommand command, […]

entity framework复杂类型的列命名约定

使用复杂类型时,默认列命名约定使用下划线。 这意味着以这种方式定义类型: [ColmplexType] public class Contact { string Email {get;set;} string Post {get;set;} } public EntityN { //… public Contact Contact {get;set;} } 我们将获得以这种方式命名的列 Contact_Email nvarchar(max) Contact_Post nvarchar(max) 我们当然可以使用ColumnAttribute或Context.Properties映射分别配置每个列名,但是我们是否有可能创建命名约定,因此为currnet类型配置一次所有名称? 对于一些复杂的类型,我宁愿不提及属性名称(“Contact”),其他人使用CammelCase连接名称和属性,永远不会使用undersore。 讨论: 这工作(创建特定表的配置信息) public class CustomComplexTypeAttributeConvention : ComplexTypeAttributeConvention { public override void Apply(ConventionTypeConfiguration configuration, ComplexTypeAttribute attribute) { Properties().Where(pi => pi.DeclaringType == typeof(Contact)) .Configure(p => p.HasColumnName(p.ClrPropertyInfo.Name) ); […]

使用Linq to Entities在一次操作中获取COUNT和SKIP TAKE

我在Linq to Entities供电数据访问层中进行了数据调用,该层旨在进行分页调用。 在这样做时,我需要选择数据的一个子集,比如50行,但也要获得所有匹配的计数,以了解要分页的总匹配数。 目前,我正在做以下事情: var queryResult = DatabaseContext.Table .Where(x => !x.IsDeleted) .Where(p => ( p.PropertyOne.ToLower().Contains(query) || p.PropertyTwo.ToLower().Contains(query) )); int count = queryResult.Count(); var returnData = queryResult .OrderBy(i => i.ID) .Skip(start).Take((length)) .Select(y => new ObjectDTO { PropertyOne = y.PropertyOne, PropertyTwo = y.PropertyTwo } .AsEnumerable(); 这导致两个昂贵的数据库操作。 由于某种原因, COUNT操作实际上需要比SELECT操作更长的时间。 有没有办法在同一个操作中获取计数和子集? 我的逻辑流程说我们做了以下事情: 看表 在表中查找与条件匹配的项目 获取所有比赛的计数 返回匹配的编号子集 这似乎可以在一次操作中,但我无法弄清楚如何。 尝试一,慢 […]

如何找到与导航属性相关的Id属性?

对于我正在使用Entity Framework的项目,我希望能够枚举给定对象实例的所有导航属性(假设它是由EF生成的对象)。 从那里我想获得每个导航属性的相关Id属性。 例如,如果我得到Person类的实例,我希望能够找到它的名为Address and Boss的导航属性。 对于那两个导航属性,我想“查找”名为AddressId和BossId的相关Id属性。 我需要那些Id属性,所以我可以在不同的数据库上运行查询,该数据库没有相同的外键但具有完全相同的ID。 到目前为止,我已经想出了一种方法来获取由EF生成的随机对象实例的RelationshipManager 。 在调试时,我可以通过Manager的Relationships属性获取外键关系。 但我只能获得导航属性名称。 所以我可以看到有一个FK_Person_Address与名为Address的导航属性相关,但我找不到AddressId 。 所以我的问题是,我如何动态地(不知道Person类的布局)发现与Address相关的AddressId属性? 我知道外键关系可能在关系的另一端有Id属性( Boss指向Person而不是Person有BossId )。 在那种情况下,当我正在检查一个Person实例时,我仍然想发现Boss有一个PersonId 。

ObjectContext.GetObjectType(e.GetType())不返回POCO实体的实体类型

ObjectContext.GetObjectType方法应返回“与指定类型的代理对象关联的POCO实体的实体类型” 那么为什么我的代码只返回代理? 我正在使用entity framework6发布候选版 //Soft delete var e = Context.Set().Find(id); e.IsDeleted = true; InsertOrUpdate(e); Type t = System.Data.Objects.ObjectContext.GetObjectType(e.GetType()); string name = t.Name; //Property_6C887DE7274181E6E99D6FCF2C21BDD59E226F99B0064F59954E70062C135331 //Surely I shouldn’t have to use Substring here? name = name.Substring(0, name.IndexOf(“_”)).ToSpacedTitleCase(); string message = name + ” deleted”;

EF 6懒惰加载禁用,但儿童记录负载无论如何

我先使用EF6代码。 有两个表, Lesson和LessonSections 。 LessonSections表具有LessonSections的外键 这是Lesson类,没有删除任何重要字段: public partial class Lesson { [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”, “CA2214:DoNotCallOverridableMethodsInConstructors”)] public Lesson() { LessonSections = new HashSet(); } [StringLength(50)] public string Id { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage(“Microsoft.Usage”, “CA2227:CollectionPropertiesShouldBeReadOnly”)] public virtual ICollection LessonSections { get; set; } } 以下是我启动数据模型的方法: var db = new Touch_Type_Trainer_DB.DataModel(); db.Configuration.ProxyCreationEnabled = false; db.Configuration.LazyLoadingEnabled = false; 在我第一次调用数据库以检索数据库中的第一课后,生成的对象没有LessonSections 然后我再次调用将这些部分检索到一个单独的对象中。 (它们必须位于单独的对象中,因为我想将它们序列化为JSON字符串,并且如果我使用标准EF […]

是否真的不可能在EF中更新子集合(也就是非hacky方式)?

假设您的实体中有这些类。 public class Parent { public int ParentID { get; set; } public virtual ICollection Children { get; set; } } public class Child { public int ChildID { get; set; } public int ParentID { get; set; } public virtual Parent Parent { get; set; } } 并且您有一个用户界面来更新Parent及其Children ,这意味着如果用户添加新Child则必须插入,如果用户编辑现有Child则需要更新,如果用户删除了Child那么必须删除。 现在很明显,如果您使用以下代码 public void Update(Parent obj) […]