标签: ef-code-first

实体框架中的主/外键

我在MVC 3应用程序中创建了一个实体类.名为RegistryId的属性之一是主键和外键.如何创建列主键和外键?我没有使用EF ORM设计师.我正在手工编写课程.

entity-framework-4 ef-code-first entity-framework-4.1

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

EF6.X中的EntityFramework代码第一个FluentAPI DefaultValue

如何使用EntityFramework Code First FluentAPI为bool属性设置默认值?

就像是:

Property(l => l.PropertyFlag).HasColumnType("bit").DefaultValue(1);
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-code-first ef-fluent-api

31
推荐指数
2
解决办法
3万
查看次数

按实体框架中的ID获取元素列表

如何通过ID获取另一个列表中的所有元素?例如; 我有List角色; 我想通过他们的Id获取此列表中数据库中的所有角色.

我正在使用代码优先.

我做了这个,它扔了一个错误:

var roles = db.Roles.Where(r => user.Roles.Any(ur => ur.RoleId == r.RoleId));
Run Code Online (Sandbox Code Playgroud)

RoleId 是int类型.

错误:

无法创建"SampleMVC.Domain.Role"类型的常量值.在此上下文中仅支持原始类型(例如Int32,String和Guid').

.net c# ef-code-first entity-framework-4.1

30
推荐指数
2
解决办法
6万
查看次数

如何防止EF"在创建模型时无法使用上下文"错误?

看看我的Elmah错误日志,我看到InvalidOperationException来自Entity Framework 的一些s处理:

The context cannot be used while the model is being created.
Run Code Online (Sandbox Code Playgroud)

这是Nuget最新的EF CodeFirst库.我能够在网上找到的唯一信息是,它是由数据上下文作为单例引起的,这当然不是我的情况.在我的Windsor安装程序中,我的EF工作单元结构正在注册:

container.Register(Component.For<IUnitOfWork>()
                            .ImplementedBy<EFUnitOfWork>()
                            .LifeStyle
                            .PerWebRequest);
Run Code Online (Sandbox Code Playgroud)

我能够通过在VS中点击F5来重新创建错误以启动调试会话,并且当IIS正在启动时,将第二个网页加载到调试会话.

我怀疑这是因为用户试图访问系统而Asp.net由于缺乏活动而卸载,这是有道理的,因为我的产品目前处于非常小的beta测试中.但是,由于真实的人正在使用带有实时数据的网站,我需要尽可能少的错误.

有谁知道如何防止这种情况发生?


编辑:我更新了我的windsor控制器,现在包含以下代码:

        container.Register(Component.For<IUnitOfWork>().ImplementedBy<EFUnitOfWork>().LifeStyle.PerWebRequest);
        using (var context = new MyJobLeadsDbContext())
        {
            context.Set<UnitTestEntity>().Any();
        }
Run Code Online (Sandbox Code Playgroud)

但是,当我在IIS加载应用程序时尝试执行第二个Web请求时,仍会发生上一个错误


编辑2:根据要求,这是堆栈

   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
   at MyApp.DomainModel.Queries.Users.UserByEmailQuery.Execute() in C:\Users\KallDrexx\Documents\Projects\MyApp\MyApp.DomainModel\Queries\Users\UserByEmailQuery.cs:line 44
   at MyApp.Infrastructure.MyAppMembershipProvider.GetUser(String email, Boolean userIsOnline) in C:\Users\KallDrexx\Documents\Projects\MyApp\MyApp\Infrastructure\MyAppMembershipProvider.cs:line 102
   at System.Web.Security.Membership.GetUser(String username, Boolean userIsOnline)
   at System.Web.Security.Membership.GetUser()
   at MyApp.MyAppBaseController.Initialize(RequestContext …
Run Code Online (Sandbox Code Playgroud)

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

30
推荐指数
2
解决办法
3万
查看次数

实体框架创建外键对象,而不是使用已经可用的对象

我当前的项目基于Entity Framwork代码优先.我有三种类型:Task,TaskType和Module.

    public class Task
    {
        public int ID { get; set; }
        public Module Module { get; set; }
        public TaskType Type { get; set; }
    }

    public class TaskType
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    public class Module
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

在表内为Task-type定义了外键关系.

我的问题是,当我尝试创建一个链接到已经可用的TaskType和Module对象(ID = 1)的新Task对象时,这些对象将在其相应的表中创建为新行.

        TaskRepository repo = new TaskRepository();

        Task task = new Task(); …
Run Code Online (Sandbox Code Playgroud)

ef-code-first entity-framework-4.1

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

任何人都可以发现为什么我一直在测试EF 5测试版时遇到此错误

安装了visual studio 11 beta,想要测试EF 5测试版但是仍然遇到这个错误.

找不到方法:'Void System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(Boolean)'.

Project是一个新的空白MVC3应用程序,下面是一些代码,用于说明错误是如何发生的.

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class EFDbContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
}

public class HomeController : Controller
{
    protected EFDbContext Db = new EFDbContext();

    public ActionResult Index()
    {
        Blog B = new Blog();
        B.Name = "Test";            
        Db.Blogs.Add(B);
        Db.SaveChanges();
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

在谷歌上查找错误但没有出现,我不太确定错误是指什么.我在下面添加了一个堆栈跟踪片段以防万一它会有所帮助.

[MissingMethodException:方法未找到: '无效System.Data.Objects.ObjectContextOptions.set_UseConsistentNullReferenceBehavior(布尔值)'.] System.Data.Entity.Internal.LazyInternalContext.InitializeContext()0 System.Data.Entity.Internal.InternalContext.Initialize ()31
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型的EntityType)39
System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext()38 …

entity-framework ef-code-first .net-4.5 entity-framework-4.3

30
推荐指数
2
解决办法
2万
查看次数

实体框架代码首次延迟加载

我有两个对象类

public class User
{
    public Guid Id { get; set; }
    public string Name { get; set; }

    // Navigation
    public ICollection<Product> Products { get; set; }
}

public class Product
{
    public Guid Id { get; set; }

    // Navigation
    public User User { get; set; }
    public Guid User_Id { get; set; }

    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我使用dataContext加载用户时,我得到的产品列表为null(这没关系).

如果我在产品列表中添加"虚拟"关键字,

public virtual ICollection<Product> Products { get; set; }
Run Code Online (Sandbox Code Playgroud)

当我加载用户时,我也获得了产品列表.

为什么会这样?我认为"虚拟"关键字用于不加载实体,除非你明确这个(使用"包含"语句)

我想我错了

c# entity-framework lazy-loading entity-framework-4 ef-code-first

30
推荐指数
1
解决办法
4万
查看次数

如何使用EntityFramework 4.1 CodeFirst阻止十进制值在保存时被截断为2个位置?

可能重复:
实体框架代码优先:小数精度

我正在使用Linq-to-Entities和EntityFramework 4.1 Code First系统.保存时,我的所有decimal属性都被截断为两位小数.我可以检查被修改的对象,并且可以在调试器中看到具有正确的小数位数,并且我可以对数据库中的值进行硬编码以显示它可以接受正确的小数位数[在这种情况下decimal(4,3)] .但是当我保存该值时,它永远不会正确保存它,而是将decimal值截断为两位小数.我无法找到System.ComponentModel.DataAnnotations允许您指定精度的类.该类(已消毒),供参考:

public class Metrics
{
    public decimal? PPM { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

c# decimal ef-code-first

29
推荐指数
1
解决办法
1万
查看次数

为什么实体框架返回空List <>而不是空?

我是ASP .NET MVC世界的新手.也许,这就是我无法向自己解释对我来说是一个令人讨厌的问题的原因.

我有一节有一对多的关系.

class MyClass{
    public List<OtherClass> otherClasses {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

当我持久化这个类的一个实例时,我用空List <>填充它的关系

MyClass myClass = new MyClass(){ otherClasses = new List<OtherClass>() }
context.myClass.Add(myClass);
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试检索该实例时,出于任何原因,我尝试访问该列表,系统给我一个空参考例外...

我的问题是:为什么EF不返回空列表而不是空列表?特别是在这种情况下,我坚持使用空列表?

有什么方法可以避免验证实例是否为空?

c# entity-framework nullreferenceexception ef-code-first entity-framework-4.1

29
推荐指数
2
解决办法
2万
查看次数

EF Code First阻止使用Fluent API进行属性映射

我有一个类Product和一个复杂的类型AddressDetails

public class Product
{
    public Guid Id { get; set; }

    public AddressDetails AddressDetails { get; set; }
}

public class AddressDetails
{
    public string City { get; set; }
    public string Country { get; set; }
    // other properties
}
Run Code Online (Sandbox Code Playgroud)

是否可以防止从类AddressDetails内部映射"Country"属性Product?(因为我在Product课堂上永远不需要它)

像这样的东西

Property(p => p.AddressDetails.Country).Ignore();
Run Code Online (Sandbox Code Playgroud)

c# entity-framework fluent-interface ef-code-first

29
推荐指数
3
解决办法
2万
查看次数