标签: entity-framework-5

实体框架5.0 EntityObject生成器在Visual Studio 2012 RC中不可用?

从social.msdn.microsoft.com继续...

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/4993d0bf-94e8-4d14-aff1-3458b4ad467f?prof=required

原帖

我尝试将项目从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(尽管已卸载)有关.

  • VS 2012修复 - 没有解决问题
  • VS 2010/2012 RC卸载/重新安装 - 没有解决问题
  • VS 2012 RC安装在干净的机器上 - 没有时间atm

截图

添加代码生成模板时我期待看到的内容

我在添加代码生成模板时看到的内容

扩展库中只有DbContext

t4 entity-framework-5 visual-studio-2012

2
推荐指数
1
解决办法
5455
查看次数

如何转换'System.Linq.IQueryable <System.Collections.Generic.List <Model.Record>到List <Record>

如何检索模型中的列表?

这就是我正在尝试的:

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)

c# ef-code-first entity-framework-5

2
推荐指数
1
解决办法
6065
查看次数

T4 引擎无法识别当前项目名称空间

我正在使用 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)

.net c# t4 self-reference entity-framework-5

2
推荐指数
1
解决办法
3585
查看次数

实体框架5 + MySQL.Data 6.6.4.0导致数据插入时出现NullReferenceException

我正在使用MySQL的Entity Framework,每次我尝试插入数据时它都会给我一个NullReferenceException.

  • 我可以通过创建命令直接插入数据,但是当我使用Entity Framework时它就会爆炸.
  • 实体框架将从表或更新表中进行选择,因此这可能与主键有关

从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)

mysql entity-framework-5

2
推荐指数
1
解决办法
1570
查看次数

实体框架代码首先更新复杂类型的单个属性

我有一种奇怪的行为.我想更新复杂类型的单个属性.当我指定要使用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

2
推荐指数
1
解决办法
1779
查看次数

实体框架生成的类型不是从 EntityObject 继承的

我是 Entity Framework 的新手(我使用的是 EF5),我正在做一个测试项目来了解它。

现在我正在按照本教程实现一些 DAO 类来访问数据库(它有点旧......)

我使用实体数据模型工具生成了实体类,但现在我需要从 EntityObject 类继承所有实体。

阅读本文时,我预计会是这样,但实际上我的类仅从IObjectWithChangeTracker,继承INotifyPropertyChanged

所以我想知道我是否做错了什么......

我必须在生成工具中设置一些配置吗?

.net c# orm entity-framework entity-framework-5

2
推荐指数
1
解决办法
2126
查看次数

通用扩展方法的正确语法

我想做这个:

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.

c# generics extension-methods entity-framework-5

2
推荐指数
1
解决办法
391
查看次数

如何在Entity Framework 5中表达"有很多通过"的关系?

我试图使用Entity Framework 5来查询现有的MySQL数据库.我使用代码优先创建一个基于代码的模型,该模型按照MSDN上的本教程映射到现有数据库.

我有两张桌子:usersbuddies.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)

c# mysql entity-framework entity-framework-5

2
推荐指数
1
解决办法
3107
查看次数

如何检查我的SQL Server Express数据库是否超过10 GB的大小限制?

我正在开发一个网站,它使用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

2
推荐指数
1
解决办法
7035
查看次数

动态表达式树,用于过滤嵌套的集合属性

我正在使用实体框架并动态地使用导航属性构建查询.

对于我的大多数用例,以下工作正常:

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)

c# linq entity-framework-5

2
推荐指数
1
解决办法
4845
查看次数