从social.msdn.microsoft.com继续...
原帖
我尝试将项目从2010 EF 4.3.1(修改后的EntityObject生成器模板)迁移到vs 2012 rc,ef 5.0 rc和.Net 45 rc.痛苦地说,我的旧T4模板不再起作用了.我在哪里可以获得正确的模板将其迁移到EF 5.0?我可以编译旧项目,但在启动应用程序后,我收到"检测到不同方案"的异常.
我的贡献,仍然没有得到解决
EntityObject代码生成模板在VS 11 Beta的"干净"机器上可用,因此我认为这是一个升级问题/冲突,可能与之前安装的EF 4.2 June 2011 CTP(尽管已卸载)有关.
截图
如何检索模型中的列表?
这就是我正在尝试的:
private void cbxPlayers_SelectedValueChanged(object sender, EventArgs e)
{
List<Record> records = new List<Record>();
string selectedPlayer = cbxPlayers.SelectedItem.ToString();
using (ProgressRecordContext context = new ProgressRecordContext())
{
records = (from Player in context.Players
where Player.Name == selectedPlayer
select Player.Records).ToList<Record>();
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这不起作用,我错过了什么?
这些是他们需要的模型:
public class Player
{
[Key][DatabaseGenerated(DatabaseGeneratedOption.None)]
public int AccountNumberId { get; set; }
public string Name { get; set; }
public virtual List<Record> Records { get; set; }
}
public class Record
{
public int RecordId { get; set; } …Run Code Online (Sandbox Code Playgroud) 我正在使用 VS 2010 (C#) T4 模板来生成代码。
我需要迭代项目中的所有类型,列出实体 poco 类并生成包装器。问题是,项目名称空间未被识别。
这是解决方案的结构:
namespace MySolution.Entities
{
public class Employee { ... }
public class Department { ... }
}
// Seperate project referenceing MySolution.Entities.
namespace MySolution.Database
{
public partial class Context { ... }
// Should generate Context.cs as a partial class with after iterating Syste.Types available in MySolution.Entities.
Context.tt
}
Run Code Online (Sandbox Code Playgroud)
这是文本模板:
<#@ template language="C#" #>
<#@ Output Extension=".cs" #>
namespace MySolution.Database
{
public partial class Context:
System.Data.Entity.DbContext
{
<#
System.Type [] types …Run Code Online (Sandbox Code Playgroud) 我正在使用MySQL的Entity Framework,每次我尝试插入数据时它都会给我一个NullReferenceException.
从SaveChanges()方法抛出以下异常.
failed: System.NullReferenceException : Object reference not set to an instance of an object.
at MySql.Data.Entity.ListFragment.WriteSql(StringBuilder sql)
at MySql.Data.Entity.SelectStatement.WriteSql(StringBuilder sql)
at MySql.Data.Entity.InsertStatement.WriteSql(StringBuilder sql)
at MySql.Data.Entity.SqlFragment.ToString()
at MySql.Data.Entity.InsertGenerator.GenerateSQL(DbCommandTree tree)
at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
at System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)
at System.Data.Common.DbProviderServices.CreateCommand(DbCommandTree commandTree)
at System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand(DbModificationCommandTree commandTree)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand(UpdateTranslator translator, Dictionary`2 identifierValues)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Objects.ObjectContext.SaveChanges()
Run Code Online (Sandbox Code Playgroud)
EF 5
public decimal CreateAlertNotification2(ulong alertServiceId, …Run Code Online (Sandbox Code Playgroud) 我有一种奇怪的行为.我想更新复杂类型的单个属性.当我指定要使用IsModified更新的属性(某些属性为true而某些属性为false)时,我没有更新任何内容.如果我没有指定复杂类型的属性,则更新复杂属性的每个字段.
public class MyEntity
{
public MyComplexClass Property1 { get; set; }
}
//... The code below doesn't work, in fact it update nothing
var entityLocal = this.Set<MyEntity>().Local.SingleOrDefault(d => d.Id == entity.Id);
if (entityLocal == null)
{
entityLocal = this.Set<MyEntity>().Attach(entity);
}
this.ChangeTracker.Entries<MyEntity>().Single(d => d.Entity == entityLocal).State = EntityState.Modified;
this.Entry(entity).Property(s => s.Property1.SubProperty1).IsModified = true;
this.Entry(entity).Property(s => s.Property1.SubProperty2).IsModified = false;//This seam to remove all update of the complex type...?
this.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
这产生:
update [dbo].[MyEntity]
set @p = 0
where (([Id] = @0))
Run Code Online (Sandbox Code Playgroud)
如果我没有将SubMperty2的IsModified指定为false,我在SQL事件探查器中有以下内容: …
c# entity-framework complextype ef-code-first entity-framework-5
我想做这个:
public static void SetStringsToBeNonUnicode(this EntityTypeConfiguration<T> config)
{
}
Run Code Online (Sandbox Code Playgroud)
编译器不喜欢那里的<T>,这个的正确语法是什么?
更多上下文,EntityTypeConfiguration是一个EntityFramework类,定义为
public class EntityTypeConfiguration<TEntityType> : StructuralTypeConfiguration<TEntityType> where TEntityType : class
Run Code Online (Sandbox Code Playgroud)
这是导致我头痛的原因.
我真正想要的是在配置dbcontext类时能够做这样的事情:
public class ReceiptEntityConfiguration: EntityTypeConfiguration<ReceiptEntity>
{
public ReceiptEntityConfiguration()
{
ToTable("vReceipt");
HasKey(r => r.ReceiptId);
this.SetStringsToBeNonUnicode(); //I want to make all string fields for this entity type (ReceiptEntity in this case) to be treated as not unicode.
...etc etc
}
}
Run Code Online (Sandbox Code Playgroud)
EF6.0使用轻量级约定来处理这个问题,但我不能将beta位用于prod.
我试图使用Entity Framework 5来查询现有的MySQL数据库.我使用代码优先创建一个基于代码的模型,该模型按照MSDN上的本教程映射到现有数据库.
我有两张桌子:users和buddies.A User有a id,a name和an email.A Buddy有a user_id和a buddy_id.A User有很多Buddies(也是Users).该buddy_id列是返回Users表中的外键.所以每个User都有很多Users通过Buddies.
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public IList<User> Buddies { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的数据库访问代码:
using (var db = new Models.fooContext()) …Run Code Online (Sandbox Code Playgroud) 我正在开发一个网站,它使用SQL Server 2008 R2 Express作为其数据库.在测试中,有大量数据和图像存储在此数据库中.
据wiki称,SQL Server Express版本的大小限制为10 GB.当我插入数据并达到限制时,将抛出什么异常?或者,如何通过代码检测接近极限问题?
我使用EF 5和代码优先的方法来插入大数据集.
c# sql-server entity-framework sql-server-2008r2-express entity-framework-5
我正在使用实体框架并动态地使用导航属性构建查询.
对于我的大多数用例,以下工作正常:
private static MethodCallExpression GetNavigationPropertyExpression<T>(string propertyName, int test,
ParameterExpression parameter, string subParameter)
{
var navigationPropertyCollection = Expression.Property(parameter, propertyName);
var childType = navigationPropertyCollection.Type.GetGenericArguments()[0];
var anyMethod = typeof(Enumerable).GetMethods().Single(m => m.Name == "Any" && m.GetParameters().Length == 2).MakeGenericMethod(childType);
var aclAttribute = GetAclAttribute(typeof(T), propertyName);
var childProperty = aclAttribute.ChildProperty;
var propertyCollectionGenericArg = childType;
var serviceLocationsParam = Expression.Parameter(propertyCollectionGenericArg, subParameter);
var left = Expression.Property(serviceLocationsParam, childProperty);
var right = Expression.Constant(test, typeof(int));
var isEqual = Expression.Equal(left, right);
var subLambda = Expression.Lambda(isEqual, serviceLocationsParam);
var resultExpression = Expression.Call(anyMethod, navigationPropertyCollection, subLambda);
return …Run Code Online (Sandbox Code Playgroud)