【问题标题】:How to get the other values that are not in the association table using Entity Framework code first如何首先使用实体​​框架代码获取不在关联表中的其他值
【发布时间】:2012-07-04 14:00:43
【问题描述】:

我有一个UserClassesScreensWorkflow 表。 Workflow 表是 UserClasses 和 Screens 表之间的关联表。这是它们的结构...

UserClasses
- UserClassID (int PK)
- UserClass (string)

Screens
- ScreenID (int PK)
- ScreenName (string)

Workflow
- UserClassificationID
- ScreenID

我在 UserClasses 表中有以下数据(ID 和名称):

- 1 UserClassification1
- 2 UserClassification2
- 3 UserClassification3
- 4 UserClassification4
- 5 UserClassification5

我在 Screens 表中有以下数据(ID 和名称):

- 1 Screen1
- 2 Screen2
- 3 Screen3

我在 Workflow 表中有以下数据(UserClassificationID 和 ScreenID):

1 1
1 2

通过查看数据,Screen3 与用户分类无关。这就是我需要的,一个与给定用户分类无关的所有屏幕的列表。我该怎么做?

关于我的设置的其他信息。我为 UserClasses 和 Screens 表定义的 2 个类:

public class UserClassification : IEntity
{
     public int Id { get; set; }
     public string Name { get; set; }
     public virtual ICollection<Screen> Screens { get; set; }
}

public class Screen : IEntity
{
     public int Id { get; set; }
     public string Name { get; set; }
     public virtual ICollection<UserClassification> UserClassifications { get; set; }
}

以下是我的数据库上下文类中使用的配置类:

class ScreenConfiguration : EntityTypeConfiguration<Screen>]
{
     internal ScreenConfiguration()
     {
          this.Property(x => x.Id).HasColumnName("ScreenID");
          this.Property(x => x.Name).HasColumnName("ScreenName");
     }
}

class UserClassificationConfiguration : EntityTypeConfiguration<UserClassification>
{
     internal UserClassificationConfiguration()
     {
          this.ToTable("UserClasses");
          this.Property(x => x.Id).HasColumnName("UserClassID");
          this.Property(x => x.Name).HasColumnName("UserClass");
          this.HasMany(i => i.Screens)
               .WithMany(c => c.UserClassifications)
               .Map(mc =>
               {
                    mc.MapLeftKey("UserClassificationID");
                    mc.MapRightKey("ScreenID");
                    mc.ToTable("Workflow");
               });
     }
}

因此,鉴于上述信息,我需要返回一个用户分类对象,其中包含与此用户分类无关的屏幕列表,在这种情况下,这将是 1 个屏幕项目的列表。我该怎么做这样的事情?

这就是我返回用户分类对象的方式,其中包含关联表中的屏幕列表:

return DbContext.UserClasses
     .Include("Screens")
     .SingleOrDefault(x => x.Id == userClassificationId);

【问题讨论】:

    标签: c# .net entity-framework entity-framework-4 entity-framework-4.1


    【解决方案1】:

    怎么样?

    from s in DbContext.Screens
    where s.UserClassifications.Count() == 0
    select s
    

    希望对你有帮助

    【讨论】:

    • 我希望返回一个用户分类对象和一个屏幕列表。这可能吗?
    • 我认为是的:s.UserClassifications.Where(uc => uc.Id == someUserId).Count() == 0.
    • 您能否也看看我的其他类似问题? stackoverflow.com/questions/11311272/…
    【解决方案2】:

    这是不可能的。急切加载 (Include) 仅适用于关联对象。您必须使用投影:

    var result = DbContext.UserClasses
                          .Where(x => x.Id == userClassificationId)
                          .Select(x => new 
                              {
                                  UserClassification = x,
                                  Screens = DbContext.Screens
                                                     .Where(s => !s.UserClasses.Any(u => u.Id)
                              })
                          .SingleOrDefault();
    

    现在您有了匿名类型,其中包含您的用户分类和所有不相关的屏幕(如果此类用户分类不存在,则为 null)。您可以根据这些信息构建新的用户分类对象(不能直接在 Linq-to-entities 查询中进行,因为 EF 不允许投影到映射类型)。

    【讨论】:

    • 您的代码存在错误:无法将类型“int”隐式转换为“bool”。这就是你拥有 u => u.Id 的地方
    • 如果可能的话,你也可以去看看我的类似问题吗? stackoverflow.com/questions/11311272/…
    猜你喜欢
    • 2013-09-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多