GVKun编程网logo

ICollection VS清单 在实体框架中

22

本文的目的是介绍ICollectionVS清单在实体框架中的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于c#–ICollection与ICollection-IColl

本文的目的是介绍ICollection VS清单 在实体框架中的详细情况,我们将通过专业的研究、有关数据的分析等多种方式,同时也不会遗漏关于c# – ICollection与ICollection- ICollection.Count和ICollection.Count之间的歧义、c# – WPF将已过滤的ObservableCollection ICollectionView绑定到Combobox、c# – `Dictionary.KeyCollection`是否实现了`IReadOnlyCollection`?、c# – 在实体框架中映射连接表的知识。

本文目录一览:

ICollection VS清单 在实体框架中

ICollection VS清单 在实体框架中

在开始设计一些Entity Framework应用程序之前,我只观看了一些网络广播。我确实没有读那么多文档,所以我现在为此感到痛苦。

List<T>在课堂上一直在使用,效果很好。

现在,我已经阅读了一些文档,并指出我应该一直在使用ICollection<T>。我改成了这个,它甚至没有引起模型上下文的改变。这是因为List<T>ICollection<T>都继承了IEnumerable<T>,而这正是EF实际需要的吗?

但是,如果是这样的话,为什么不EF文档状态,它需要IEnumerable<T>的不是ICollection<T>

无论如何,我做过的事情有什么弊端,还是我应该改变它?

答案1

小编典典

ICollection<T>之所以会使用实体框架,是因为它需要支持Add不是IEnumerable<T>接口一部分的操作。

另请注意,您
使用ICollection<T>,只是将其作为List<T>实现公开。List<T>随之带来的IList<T>ICollection<T>IEnumerable<T>

至于您所做的更改,尽管可行,但通过界面进行公开是一个不错的选择List<T>。该接口定义合同,但不定义实现。实现 可能会
改变。例如,在某些情况下,实现可能是HashSet<T>。(顺便说一下,(这是一种思维方式,您不仅可以将其用于实体框架)。一种好的面向对象的实践是针对接口而不是实现进行编程。实现可能并且将会改变。)

c# – ICollection与ICollection- ICollection.Count和ICollection.Count之间的歧义

c# – ICollection与ICollection- ICollection.Count和ICollection.Count之间的歧义

注意:这与 this other question相似,但不完全相同

我已经实现了IBusinessCollection接口.它既来自ICollection< T>又来自旧的非通用ICollection.我更喜欢转储旧的已经破坏的ICollection,但我正在使用带有CollectionView的WPF数据绑定,它希望我实现旧的非泛型IList

总结

以上是小编为你收集整理的c# – ICollection与ICollection- ICollection.Count和ICollection.Count之间的歧义全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

c# – WPF将已过滤的ObservableCollection ICollectionView绑定到Combobox

c# – WPF将已过滤的ObservableCollection ICollectionView绑定到Combobox

我想根据类型(类型AddPoint)过滤ObservableCollection到一个子集,并希望它按升序排序,没有重复.我的基类是ModelBase,w /子类AddPoint,Time,Repeat等…… ObservableCollection MotionSequenceCollection将以任何顺序填充这些类型,有些将是重复的.

我已经尝试了几次不同的时间,并在ICollectionView属性中显示它,我从’Bind subset of collection中拉出’.

可观察的收藏品

private ObservableCollection<ModelBase> _motionSequenceCollection = 
        new ObservableCollection<ModelBase>();

    public ObservableCollection<ModelBase> MotionSequenceCollection
    {
        get
        {
            return _motionSequenceCollection;
        }

        set
        {
            if (_motionSequenceCollection == value)
            {
                return;
            }

            var oldValue = _motionSequenceCollection;
            _motionSequenceCollection = value;

            // Update bindings,no broadcast
            RaisePropertyChanged();
        }
    }

    public ICollectionView Location
    {
        get
        {
             var location = CollectionViewSource.Getdefaultview(_motionSequenceCollection);

            //DOES NOT WORK.  PROBLEM: GetType() creates type of system.type() and AddPoint,which don't work.  Need a cast,or something??  
            // found at https://stackoverflow.com/questions/9621393/bind-subset-of-collection  The problem is that there is an error:
            //    Cannot apply operator '==' to operands of type 'System.Type' and 'MotionSeq.Model.AddPoint',//    candidates are:
            //          bool ==(System.Reflection.MemberInfo,System.Reflection.memberInfo) (in class MemberInfo)
            //          bool ==(System.type,System.Type) (in class Type)
            //location.Filter = p => (p as ModelBase).GetType() == AddPoint;

            //DOES NOT WORK.  PROBLEM: Affects the main collection and won't let TIME type added.
            //location.Filter = o1 => (o1 is AddPoint);

            //DOES NOT WORK.  PROBLEM: Sorts fine,but also sorts MotionSequenceCollection!!  What up w/ that!?  
            //location.sortDescriptions.Add(new SortDescription("AddPointClassName",ListSortDirection.Ascending));

            //DOES NOT WORK.  PROBLEM: MotionSequenceCollection does not update.
            //location.Filter = p => (p as ModelBase) == AddPoint;

            //DOES NOT WORK.  PROBLEM: Source is not instantiated(?) and exmaple from stackoverflow and not sure how that got there in the first place.
            //source.Filter = p => (p as ModelBase).GetType() == "AddPoint";
            //return source;

            return location;
        }
    }

解决方法

All collections have a default CollectionView. WPF always binds to a view rather than a collection. If you bind directly to a collection,WPF actually binds to the default view for that collection. This default view is shared by all bindings to the collection,which causes all direct bindings to the collection to share the sort,filter,group,and current item characteristics of the one default view.

尝试创建CollectionViewSource并设置其过滤逻辑,如下所示:

//create it as static resource and bind your ItemsControl to it
<CollectionViewSource x:Key="csv" Source="{StaticResource MotionSequenceCollection}" 
                  Filter="CollectionViewSource_Filter">
    <CollectionViewSource.GroupDescriptions>
       <PropertyGroupDescription PropertyName="YYY"/>
    </CollectionViewSource.GroupDescriptions>
    <CollectionViewSource.sortDescriptions>
         <scm:SortDescription PropertyName="YYY" Direction="Ascending"/>
    </CollectionViewSource.sortDescriptions>
</CollectionViewSource> 

private void CollectionViewSource_Filter(object sender,FilterEventArgs e)
{
    var t = e.Item as ModelBase;
    if (t != null)

    {
        //use your filtering logic here

    }
}

c# – `Dictionary.KeyCollection`是否实现了`IReadOnlyCollection`?

c# – `Dictionary.KeyCollection`是否实现了`IReadOnlyCollection`?

问题似乎很简单.虽然 documentation说它确实:
public sealed class KeyCollection : ICollection<TKey>,IReadOnlyCollection<TKey>,IEnumerable<TKey>,ICollection,IEnumerable

以下代码给出了错误:

class MyKeys<T>
{
    readonly Dictionary<T,T> dict = new Dictionary<T,T> ();
    public IReadOnlyCollection<T> Keys { get { return dict.Keys; } set; }
}

说没有来自KeyCollection< T>的转换到IReadOnlyCollection< T>.

此外,polish documentation(法国也就此而言)说它没有:

[SerializableAttribute]
public sealed class KeyCollection : ICollection<TKey>,IEnumerable

这是什么?

如果是英文文档中的错误,还有一个奖励问题:

有没有办法让Keys成为只读集合?

解决方法

Dictionary.KeyCollection当前没有实现IReadOnlyCollection.

但是,该接口已添加到.Net的下一个版本(v4.6).您可以看到使用VS 2015预览.

您还可以在this announcement中看到(下载包含v4.6所有更改的excel文件)

c# – 在实体框架中映射连接表

c# – 在实体框架中映射连接表

我正在尝试将asp.net成员资格表连接到asp.mvc 3网站.我一直在关注运动商店的Steve Sanderson的书“Pro ASP.NET MVC 3 Framework”中的教程,并将其应用于从成员资格exe生成的表格.

所以我有一个用户类,如下所示:

namespace Domain.Entities
{
    public class User
    {
        public Guid UserId { get; set; }
        public string UserName { get; set; }
        public DateTime LastActivityDate;
        public virtual ICollection<Role> Roles { get; set; }
    }

    public class Role
    {
        public Guid RoleId { get; set; }
        public string RoleName { get; set; }
    }
}

和一个看起来像这样的上下文类:

public class EfdbContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Role> Roles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().ToTable("aspnet_users");
        modelBuilder.Entity<Role>().ToTable("aspnet_roles");
    }
}

但我得到一个错误,因为我假设它正在寻找这两个表之间的连接,实际上在它们之间有一个连接表(aspnet_UsersInRoles)以避免多对多的链接,当我尝试从用户引用Role模型时,如:

var test = _repository.Users.FirstOrDefault().Roles.Count();

{“Invalid column name ‘User_UserId’.\r\nInvalid column name
‘User_UserId’.\r\nInvalid column name ‘User_UserId’.”}

有没有办法使用实体框架使用连接表将表映射到一起?添加新的ADO.NET实体数据模型并让visual studio对数据库进行逆向工程更好吗?

解决方法

您需要自定义多对多映射,如下所示
protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
       modelBuilder.Entity<User>().ToTable("aspnet_users");
       modelBuilder.Entity<Role>().ToTable("aspnet_roles");

       modelBuilder.Entity<User>()
       .HasMany(u => u.Roles).WithMany(r => r.Users)
            .Map(m =>
            {
                m.ToTable("aspnet_UsersInRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });
   }

编辑:您还需要将Users属性添加到R​​ole类.否则映射应更改为

modelBuilder.Entity<User>()
       .HasMany(u => u.Roles).WithMany()
            .Map(m =>
            {
                m.ToTable("aspnet_UsersInRoles");
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });

今天关于ICollection VS清单 在实体框架中的分享就到这里,希望大家有所收获,若想了解更多关于c# – ICollection与ICollection- ICollection.Count和ICollection.Count之间的歧义、c# – WPF将已过滤的ObservableCollection ICollectionView绑定到Combobox、c# – `Dictionary.KeyCollection`是否实现了`IReadOnlyCollection`?、c# – 在实体框架中映射连接表等相关知识,可以在本站进行查询。

本文标签: