标签: ef-code-first

使用实体框架6删除表

我正在一个使用实体框架6代码的项目中工作,我正在尝试找出从数据库中删除现已过时的表的正确方法。网上似乎没有太多有关此的信息,我知道我可以简单地从dbcontext中删除dbset,为其删除迁移文件,并从dbs中的migrations表中删除该行,但是我只是想看看是否那是唯一的方法,因为这似乎很混乱?

ef-code-first entity-framework-6

3
推荐指数
1
解决办法
4529
查看次数

忽略复杂类型的一个属性

我想忽略一个复杂类型的属性来映射到数据库,其中FinalTotal是一个计算字段.实体框架说它是不允许的,它必须是一个属性.:(

public class Sale
{
     public int      Id { get; set; }
     public DateTime DateSale { get; set; }
     public Amounts  Amounts { get; set; }
}

public class Amounts
{
     public decimal Subtotal { get; set; }
     public decimal Tax { get; set; }
     public decimal FinalTotal { get; set; }
}

public class SaleMap : EntityTypeConfiguration<Sale>
{
     public SaleMap()
     {
          Ignore(s => s.Amounts.FinalTotal);
     }
}
Run Code Online (Sandbox Code Playgroud)

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

3
推荐指数
1
解决办法
1221
查看次数

EF 6如何在生成迁移时跳过现有表

我正在尝试在现有项目中使用代码优先。我想从为新实体生成的迁移中排除现有实体。

我将所有模型放在单独的类库项目(例如Data.Models)中,并且打算通过创建另一个类库(例如Infra.EF)使用一个上下文(在其中引用了模型项目)。

这是我的DbContext的样子:

    public DbSet<ExistingEntityOne> DataOfEntityOne { get; set; }
    public DbSet<ExistingEntityTwo> DataofEntityTwo { get; set; }
    public DbSet<NewEntity> NewData { get; set; }
Run Code Online (Sandbox Code Playgroud)

抱歉,问题尚不清楚,但是我可以根据您的反馈添加更多信息。

谢谢。

entity-framework ef-code-first

3
推荐指数
1
解决办法
2004
查看次数

有界上下文和EF代码优先 - 如何将它们组合在一起?

恕我直言,最出色的DDD概念之一是应用程序中单独的上下文的能力.但我对如何将所有东西放在一起工作感到困惑.

首先,我知道有一件事与其他事无关.但我的问题完全在于Infrastructure/ORM部分.

例如,我有一个名为Procedure(医疗程序)的域对象.在注册上下文中,唯一重要的是代码名称.但是在程序管理上下文中,我有很多其他领域,比如Price,属于这个特定的上下文.

如何使用EF Code First在具有不同属性的两个具有相同名称(在不同上下文中)的实体?基本上,我想在同一个表中保存所有字段,但在一个上下文中只检索2个字段,在另一个上下文中检索所有字段.我怎么能做到这一点?

.net domain-driven-design entity-framework ef-code-first bounded-contexts

3
推荐指数
1
解决办法
1797
查看次数

每个表只允许一个标识列

我有这门课:

public  class Comment
{
    public virtual int Id { get; set; }
    public string Body { get; set; }
    public DateTime AddDate { get; set; }
    [ForeignKey("PostId")]
    public virtual Post Post { get; set; }
    public int PostId { get; set; }
    public virtual bool IsApproved { get; set; }
    public virtual int LikeCount { get; set; }
    public int? ParentId { get; set; }
    public virtual Comment Parent { get; set; }
    public ICollection<Comment> Children { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# fluent code-first ef-code-first

3
推荐指数
1
解决办法
6922
查看次数

Code First选择数据 - 相关实体始终返回null

我正在尝试使用Entity Framework实现一个系统.这是我的两个班级

一级

public class ConsumableGood
{
    public ConsumableGood() { }

    public Guid ConsumableGoodId { get; set; }
    public string ConsumableGoodName { get; set; }
    public string ConsumableGoodPrice { get; set; }
    public virtual Category Category { get; set; }

    public ICollection<ConsumableGoodsStock> ConsumableGoodsStocks { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

第二类

public class ConsumableGoodsStock
{
    public ConsumableGoodsStock()
    {

    }

    public Guid ConsumableGoodsStockId { get; set; }
    public double ConsumableGoodPriceIn { get; set; }
    public double ConsumableGoodPriceOut { get; set; }
    public int …
Run Code Online (Sandbox Code Playgroud)

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

3
推荐指数
1
解决办法
1618
查看次数

ASP.NET MVC多对多关系,使用"My Own"表

我对使用Code First方法和实体框架相当新,我知道我有很多关系,比如下面的实体,EF会自动创建中间表:

class Post {
  ...
  public virtual ICollection<Category> Categories {get; set;}
  ... 
}

class Category {
   ...
   public virtual ICollection<Post> Posts {get; set;}
   ...
}
Run Code Online (Sandbox Code Playgroud)

但是,如果在中间表中我需要有额外的数据字段,一种可能的方式(我目前喜欢,也许是因为我不知道更好的方法)将定义我自己的新实体,如:

class Posts_Categories {
   public int Id {get; set;}
   public int CategoryId {get; set;}
   public int PostId {get; set;}
   public string Exrtafield1 {get; set;}
   public int ex extraField2 {get; set;}
   ...
   public virtual Post Post {get; set;}
   public virtual Category Category {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

使用这种方法,EF确实创建了我的自定义中间表,但它还创建了另一个自己的名为"PostsCategories",它只包含Post_Id的外键和Category_Id的另一个外键.

如何让它不创建额外的一个并使用我定义的那个?这是管理多对多关系与额外数据字段的好方法吗?

asp.net-mvc many-to-many ef-code-first entity-framework-6

3
推荐指数
1
解决办法
2530
查看次数

LINQ to Entities不支持指定的类型成员'Title'

Title在Linq to Entity中使用属性时遇到错误:

LINQ to Entities不支持指定的类型成员'Title'.仅支持初始值设定项,实体成员和实体导航属性.

我的查询是:

        var db = FaraWorkspaceEntity.GetEntity();

        var query = from item in db.ProjectManagers
                    where item.ProjectID == projectID
                    select new UserListItem
                    {
                        ID = item.UserID,
                        Title = item.User.Title // Here is error
                    };

        return query.ToList();



    public class User : IdentityUser<string, UserLogin, UserRole, UserClaim>
    {
        [Required]
        [Display(Name = "???")]
        [StringLength(50)]
        public string Firstname { get; set; }

        [Required]
        [Display(Name = "??? ????????")]
        [StringLength(50)]
        public string Lastname { get; set; }

        public string Title
        {
            get …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework ef-code-first

3
推荐指数
1
解决办法
1432
查看次数

为什么Entity Framework会创建一个不映射到任何字段的额外列,并且始终为null?

我的POCO设置如下:

public class Notification : BaseClass
{

    public User Receiver { get; set; }

    [Required, Index]
    public long ReceiverID { get; set; }

    public virtual User ContextualUser { get; set; }

    public long? ContextualUserID { get; set; }

    public virtual User Actor { get; set; }

    public long? ActorID { get; set; }

    public string Content { get; set; }

    public string Uri { get; set; }

    [Required]
    public string Type { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

我的基类有只有三个属性:ID …

sql-server entity-framework ef-code-first

3
推荐指数
1
解决办法
1172
查看次数

如何更新实体框架7迁移和数据库 - 代码优先

在ef 7中,我使用命令'dnx ef migrations add Initial'创建了初始迁移.当我运行应用程序数据库是为我创建的,一切都很好.

然后我更新了我的实体对象(CSharp文件).现在该怎么办:

  1. 更新现有迁移?(或者我必须添加新的?)
  2. 更新已创建的数据库

我收到错误:数据库中已存在一个对象.

此外,任何资源,我可以首先找到实体框架代码的良好实际示例,因为我正在努力加快速度.

谢谢,Javed

c# ef-code-first ef-migrations entity-framework-core asp.net-core

3
推荐指数
1
解决办法
4977
查看次数