相关疑难解决方法(0)

如何在Entity Framework中创建多对多映射?

在这种情况下,我有2个实体,如Contract,Media.

public class Media : Entity
{
    public string Name {get; set;}
    public bool Enabled
    *//other properties can be ignored..*
}

public class Contract : Entity
{
    public string Code {get; set;}
    *//other properties can be ignored..*
}
Run Code Online (Sandbox Code Playgroud)

合同有很多媒体,似乎它们是多对多的.

但!!首先,在ef代码中,我需要在ContractMedia表中增加3个字段(如果自动生成).例如StartDate,EndDate和Price.这些无法在媒体实体中添加.

如何在这种情况下映射?

c# many-to-many entity-framework

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

实体框架CodeFirst与其他信息的多对多关系

我有以下型号:

class Contract
{
   string ContractID{get;set;}
   ICollection<Part> Parts{get;set;}
}

class Part
{
   string PartID{get;set;}
   ICollection<Contract> Contracts{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

问题是Part和Contract之间的关系还包含以下附加信息:

class ContractParts
{ 
   Contract{get;set;}
   Part{get;set;}
   Date{get;set;} //additional info
   Price{get;set;} //additional info
}
Run Code Online (Sandbox Code Playgroud)

我将如何为此编写实体上下文?

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

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

如何使用实体框架流畅的API配置多对多关系

我试图首先在EF代码中建立多对多关系,但默认约定是错误的.以下类描述了这种关系:

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

class Account
{        
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

一个帐户可以有许多产品.

但是,EF约定将创建DB表,如下所示:

Products Table
--------------
Id
Name
Account_Id  <- What is this?

Accounts Table
--------------
Id
Name
Run Code Online (Sandbox Code Playgroud)

这看起来不像是一个多对多的表结构?如何配置流畅的API以反映关系并创建中间表:

AccountProducts Table
---------------------
Account_Id
Product_Id
Run Code Online (Sandbox Code Playgroud)

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

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

实体框架6代码优先 - 通过注释多对多的方式

是否有可能在实体框架6中创建具有代码优先和注释的单向多对多关联?例:

class Currency
{
    public int id { get; set; }
}

class Country
{
    public int id { get; set; }

    // How i can annotate this property to say EF that it is many-to-many
    // and it should create mapping table?
    // I don't need navigation property to Country in Currency class!
    public virtual IList<Currency> currencies { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

在Java + JPA注释中,我可以通过这种方式实现我需要的东西:

@OneToMany
@JoinTable(name = "MAPPING_TABLE", joinColumns = {
    @JoinColumn(name = "THIS_ID", referencedColumnName = "ID")
}, …
Run Code Online (Sandbox Code Playgroud)

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

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

实体框架 - 渴望加载相关实体

对不起,标题不是更具体 - 我不知道如何简洁地描述.我有Trips和Location,它们具有多对多的关系 - 直截了当,除了Locations不需要知道使用它们的Trips.我创建了这些实体来代表这个:

public class Trip
{
    public int TripId { get; set; }
    public virtual IList<TripLocation> TripLocations { get; set; }
}

public class TripLocation
{
    public int TripId { get; set; }
    public int LocationId { get; set; }

    public virtual Location Location { get; set; }
}

public class Location
{
    public int LocationId { get; set; }
    // Note: Intentionally no collection of Trips
}
Run Code Online (Sandbox Code Playgroud)

我可以让Trip快速加载它的TripLocations但我无法让TripLocations急于加载他们的位置.我已经尝试了一些流畅的配置组合和"包含"在查询中,如

IQueryable<Trip> query = from trip in context
                              .Include(r …
Run Code Online (Sandbox Code Playgroud)

entity-framework eager-loading ef-code-first

9
推荐指数
1
解决办法
6671
查看次数

首先映射EF 4.1代码中的关联表

我不确定如何映射下面的表格EF 4.1 code first以及我需要表示表格的对象.我如何检索产品规格列表?

我目前只Product上课了.

Products Table:
Id
Name
IsActive

ProductSpecification Table:
ProductId
SpecificationId

Specifications Table:
Id
Name
IsActive
Run Code Online (Sandbox Code Playgroud)

ProductSpecifications是一个关联表.我的上下文类中也定义了以下内容:

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

编辑

请参阅我更新的原始帖子.我更改了产品和规格表的ID.

在我的上下文课中,我有以下内容:

public DbSet<Product> Products { get; set; }
public DbSet<Specification> Specifications { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我的存储库中,我有以下内容:

public Product GetById(int id)
{
     return db.Products
          .Include("Specifications")
          .SingleOrDefault(x => x.Id == id);
}
Run Code Online (Sandbox Code Playgroud)

我的Product class(部分):

public class Product : IEntity
{
     public int Id { get; set; …
Run Code Online (Sandbox Code Playgroud)

ado.net entity-framework entity-framework-4.1

6
推荐指数
1
解决办法
6268
查看次数

使用外键引用映射多对多关系

对于精通EF的用户来说,这应该是一个简单的问题.

我有以下模式(在我的脑海中)表示表之间的关系应该如何.

[FooBar]      [Foo]          [Bar]

FooId PK,FK   Id PK          Id PK
BarId PK,FK   BarId FK       Name
IsRead        Name           Description
              Description    
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用EF代码生成模式时,首先它无法解释实体之间的关系,因为我已解释它们(FooId[bar]表中添加外键)并且无法完全创建[FooBar]桥表.

如果有人可以指导我如何使用EF4代码实现上述架构 - 首先我很感激.只要创建了所需的数据库模式,解决方案是否涉及我的POCO模型上的属性,流畅的配置或两者的混合都无关紧要.


POCO型号:

public class Foo
{
    public int Id { get; set; }
    public string Text { get; set; }
    public string Description { get; set; }
    public int BarId { get; set; }

    public Bar Bar { get; set; } /* bar entity */

    public virtual ICollection<Bar> BridgedBars …
Run Code Online (Sandbox Code Playgroud)

.net c# sql-server entity-framework relational-database

6
推荐指数
1
解决办法
6769
查看次数

多个添加的实体可能在数据库种子上具有相同的主键

假设我使用实体框架6为asp.net mvc 5应用程序提供了以下模型结构

class Athlete {
 int AthleteID {get; set;}
 List<YearsAsAthlete> YearsAsAthlete {get;set;}
}

class YearsAsAthlete {
 int YearsAsAthleteID {get;set;}
 int AthleteID {get;set;}
 [ForeignKey("AthleteID")]
 Athlete Athlete {get;set;}
 List<ContractRevenue> ContractRevenue {get;set;}
 List<AdvertisementRevenue> AdvertisementRevenue {get;set;}
}

class ContractRevenue {
 int ContractRevenueID {get;set;}
 int YearsAsAthleteID {get;set;}
 [ForeignKey("YearsAsAthleteID")]
 YearsAsAthlete YearsAsAthlete {get;set;}

 List<RevenueAmounts> RevenueAmounts {get;set;}
}

class AdvertisementRevenue {get;set;}
 int AdvertisementRevenueID {get;set;}
 int YearsAsAthleteID {get;set;}
 [ForeignKey("YearsAsAthleteID")]
 YearsAsAthlete YearsAsAthlete {get;set;}

 List<RevenueAmounts> RevenueAmounts {get;set;}
}

class RevenueAmounts {
 int RevenueAmountsID {get;set;}
 int AmountPaid {get;set;}
 date DateOfPayment {get;set;} …
Run Code Online (Sandbox Code Playgroud)

c# mysql asp.net-mvc entity-framework

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

EF代码首先 - 多对多关系映射表与额外列

我有一个用户模型和一个组模型.用户和组共享多对多关系.当我将其转换为表格时,我希望有一个映射表.我使用以下来实现这一目标.

modelBuilder.Entity<UserGroup>()
        .HasMany(a => a.Users)
         .WithMany(b => b.UserGroup)

         .Map(mc =>
         {

             mc.ToTable("UserUserGroupMapping");
             mc.MapLeftKey("UserId");
             mc.MapRightKey("UserGroupId");

         });
Run Code Online (Sandbox Code Playgroud)

这将创建一个以UserId和UserGroupId为列的表.但是我几乎没有挑战,

我希望能够在此表中添加一个Identity列,并向表中添加一些审计列(例如:创建者,创建日期).我不知道该怎么做.

有人能帮我一下吗?

谢谢

entity-framework ef-code-first ef-migrations entity-framework-6

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

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
查看次数

多对多排序

ASP.NET MVC4,C#.

我有两个模型A和B.它们之间有很多对多的关系.

假设A有一个B的集合.我想在A上对B的集合进行排序并将其保存在数据库中.

正确的数据库设计是为桥接表A_B添加一个额外的属性.但是,实体框架不允许这样做.允许在实体框架中对多对多关系进行排序的最佳解决方案是什么?

谢谢.

c# many-to-many entity-framework

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