Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态数据中的多对多关系_C#_Sql_Dynamic Data - Fatal编程技术网

C# 动态数据中的多对多关系

C# 动态数据中的多对多关系,c#,sql,dynamic-data,C#,Sql,Dynamic Data,我刚刚创建了一个动态数据项目,我的数据库项目中有很多多对多关系和元素。最大的问题是,当我想要编辑/插入一个元素时,因为每个多对多关系都会在复选框列表中“转换”。因此,对于我要插入/编辑的实体,它将导致一个高度约4000px的页面,其中充满了多对多元素 这样(假设产品和类别之间存在多对多关系): 我现在想的是创建一个名为“GridViewCheckBox”的自定义字段,其中有一个GridView,我可以在其中加载实体,在实体名称前面有一个复选框(因为GridView也有分页功能) 我的问题是: 1

我刚刚创建了一个动态数据项目,我的数据库项目中有很多多对多关系和元素。最大的问题是,当我想要编辑/插入一个元素时,因为每个多对多关系都会在复选框列表中“转换”。因此,对于我要插入/编辑的实体,它将导致一个高度约4000px的页面,其中充满了多对多元素

这样(假设产品和类别之间存在多对多关系):

我现在想的是创建一个名为“GridViewCheckBox”的自定义字段,其中有一个GridView,我可以在其中加载实体,在实体名称前面有一个复选框(因为GridView也有分页功能)

我的问题是:

1) 可以跟随吗

2) 如何使gridview加载我的特定多对多关系


因为我有产品类别,但我也可以有汽车修理厂,我也可以有个人城市等等。

这就是我目前在我的许多编辑网站ascx.cs中的内容

public void Page_Load(object sender, EventArgs e) {
    // Register for the DataSource's updating event
    EntityDataSource ds = (EntityDataSource)this.FindDataSourceControl();

    // This field template is used both for Editing and Inserting
    ds.Updating += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);
    ds.Inserting += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);


}

void DataSource_UpdatingOrInserting(object sender, EntityDataSourceChangingEventArgs e) {
    MetaTable childTable = ChildrenColumn.ChildTable;

    // Comments assume employee/territory for illustration, but the code is generic

    // Get the collection of territories for this employee
    RelatedEnd entityCollection = (RelatedEnd)Column.EntityTypeProperty.GetValue(e.Entity, null);

    // In Edit mode, make sure it's loaded (doesn't make sense in Insert mode)
    if (Mode == DataBoundControlMode.Edit && !entityCollection.IsLoaded) {
        entityCollection.Load();
    }

    // Get an IList from it (i.e. the list of territories for the current employee)
    // REVIEW: we should be using EntityCollection directly, but EF doesn't have a
    // non generic type for it. They will add this in vnext
    IList entityList = ((IListSource)entityCollection).GetList();

    // Go through all the territories (not just those for this employee)
    foreach (object childEntity in childTable.GetQuery(e.Context)) {

        // Check if the employee currently has this territory
        bool isCurrentlyInList = entityList.Contains(childEntity);

        // Find the checkbox for this territory, which gives us the new state
        string pkString = childTable.GetPrimaryKeyString(childEntity);
        ListItem listItem = CheckBoxList1.Items.FindByValue(pkString);
        if (listItem == null)
            continue;

        // If the states differs, make the appropriate add/remove change
        if (listItem.Selected) {
            if (!isCurrentlyInList)
                entityList.Add(childEntity);
        }
        else {
            if (isCurrentlyInList)
                entityList.Remove(childEntity);
        }
    }
}

protected void gvMyGrid_DataBound(object sender, EventArgs e)
{




}

protected void CheckBoxList1_DataBound(object sender, EventArgs e) {
    MetaTable childTable = ChildrenColumn.ChildTable;

    // Comments assume employee/territory for illustration, but the code is generic

    IList entityList = null;
    ObjectContext objectContext = null;

    if (Mode == DataBoundControlMode.Edit) {
        object entity;
        ICustomTypeDescriptor rowDescriptor = Row as ICustomTypeDescriptor;
        if (rowDescriptor != null) {
            // Get the real entity from the wrapper
            entity = rowDescriptor.GetPropertyOwner(null);
        } else {
            entity = Row;
        }

        // Get the collection of territories for this employee and make sure it's loaded
        RelatedEnd entityCollection = Column.EntityTypeProperty.GetValue(entity, null) as RelatedEnd;
        if (entityCollection == null) {
            throw new InvalidOperationException(String.Format("The ManyToMany template does not support the collection type of the '{0}' column on the '{1}' table.", Column.Name, Table.Name));
        }
        if (!entityCollection.IsLoaded) {
            entityCollection.Load();
        }

        // Get an IList from it (i.e. the list of territories for the current employee)
        // REVIEW: we should be using EntityCollection directly, but EF doesn't have a
        // non generic type for it. They will add this in vnext
        entityList = ((IListSource)entityCollection).GetList();

        // Get the current ObjectContext
        // REVIEW: this is quite a dirty way of doing this. Look for better alternative
        ObjectQuery objectQuery = (ObjectQuery)entityCollection.GetType().GetMethod(
            "CreateSourceQuery").Invoke(entityCollection, null);
        objectContext = objectQuery.Context;
    }

    // Go through all the territories (not just those for this employee)
    foreach (object childEntity in childTable.GetQuery(objectContext)) {
        MetaTable actualTable = MetaTable.GetTable(childEntity.GetType());
        // Create a checkbox for it
        ListItem listItem = new ListItem(
            actualTable.GetDisplayString(childEntity),
            actualTable.GetPrimaryKeyString(childEntity));

        // Make it selected if the current employee has that territory
        if (Mode == DataBoundControlMode.Edit) {
            listItem.Selected = entityList.Contains(childEntity);
        }

        CheckBoxList1.Items.Add(listItem);
    }
}

public override Control DataControl {
    get {
        return gvMyGrid;
    }
}
public void页面加载(对象发送方,事件参数e){
//注册数据源的更新事件
EntityDataSource ds=(EntityDataSource)this.FindDataSourceControl();
//此字段模板用于编辑和插入
ds.updateing+=新的EventHandler(DataSource\u UpdatengorInserting);
ds.Inserting+=新的事件处理程序(DataSource\U UpdatingOrInserting);
}
void DataSource_updatengorInserting(对象发送方,EntityDataSourceChangingEventArgs e){
MetaTable childTable=ChildrenColumn.childTable;
//注释假设员工/地区用于说明,但代码是通用的
//获取此员工的区域集合
RelatedEnd entityCollection=(RelatedEnd)Column.EntityTypeProperty.GetValue(e.Entity,null);
//在编辑模式下,确保已加载(在插入模式下没有意义)
if(Mode==DataBoundControlMode.Edit&!entityCollection.IsLoaded){
entityCollection.Load();
}
//从中获取IList(即当前员工的区域列表)
//评论:我们应该直接使用EntityCollection,但EF没有
//它的非泛型类型。他们将在vnext中添加它
IList entityList=((IListSource)entityCollection.GetList();
//检查所有区域(不仅仅是该员工的区域)
foreach(childTable.GetQuery(e.Context)中的对象childEntity){
//检查员工当前是否拥有此区域
bool isCurrentlyInList=entityList.Contains(childEntity);
//找到这个区域的复选框,它为我们提供了新的状态
字符串pkString=childTable.GetPrimaryKeyString(childEntity);
ListItem ListItem=CheckBoxList1.Items.FindByValue(pkString);
如果(listItem==null)
继续;
//如果状态不同,请进行适当的添加/删除更改
如果(listItem.Selected){
如果(!isCurrentlyInList)
entityList.Add(子实体);
}
否则{
如果(isCurrentlyInList)
entityList.Remove(子实体);
}
}
}
受保护的void gvMyGrid_数据绑定(对象发送方,事件参数e)
{
}
受保护的void CheckBoxList1_数据绑定(对象发送方,事件参数e){
MetaTable childTable=ChildrenColumn.childTable;
//注释假设员工/地区用于说明,但代码是通用的
IList entityList=null;
ObjectContext=null;
if(Mode==DataBoundControlMode.Edit){
对象实体;
ICustomTypeDescriptor rowDescriptor=作为ICustomTypeDescriptor的行;
if(行描述符!=null){
//从包装器中获取真实实体
entity=rowDescriptor.GetPropertyOwner(null);
}否则{
实体=行;
}
//获取此员工的区域集合并确保已加载
RelatedEnd entityCollection=Column.EntityTypeProperty.GetValue(实体,null)作为RelatedEnd;
if(entityCollection==null){
抛出新的InvalidOperationException(String.Format(“ManyToMany模板不支持{1}”表上的{0}列的集合类型。”,column.Name,table.Name));
}
如果(!entityCollection.IsLoaded){
entityCollection.Load();
}
//从中获取IList(即当前员工的区域列表)
//评论:我们应该直接使用EntityCollection,但EF没有
//它的非泛型类型。他们将在vnext中添加它
entityList=((IListSource)entityCollection).GetList();
//获取当前ObjectContext
//评论:这是一个相当肮脏的做法。寻找更好的替代方法
ObjectQuery ObjectQuery=(ObjectQuery)entityCollection.GetType().GetMethod(
“CreateSourceQuery”).Invoke(entityCollection,null);
objectContext=objectQuery.Context;
}
//检查所有区域(不仅仅是该员工的区域)
foreach(childTable.GetQuery(objectContext)中的对象childEntity){
MetaTable actualTable=MetaTable.GetTable(childEntity.GetType());
//为它创建一个复选框
ListItem ListItem=新ListItem(
actualTable.GetDisplayString(childEntity),
GetPrimaryKeyString(childEntity));
//如果当前员工拥有该区域,则将其选中
if(Mode==DataBoundControlMode.Edit){
listItem.Selected=entityList.Contains(childEntity);
}
CheckBoxList1.Items.Add(listItem);
}
}
公共覆盖控制数据控制{
得到{
返回gvMyGrid;
}
}

我知道这是非常非常古老的,但它确实停止了好几天,可以看看为什么我的许多人总是无法获得现场数据:我使用DD的时间只有大约1-2周,比我以前的时间要长
public void Page_Load(object sender, EventArgs e) {
    // Register for the DataSource's updating event
    EntityDataSource ds = (EntityDataSource)this.FindDataSourceControl();

    // This field template is used both for Editing and Inserting
    ds.Updating += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);
    ds.Inserting += new EventHandler<EntityDataSourceChangingEventArgs>(DataSource_UpdatingOrInserting);


}

void DataSource_UpdatingOrInserting(object sender, EntityDataSourceChangingEventArgs e) {
    MetaTable childTable = ChildrenColumn.ChildTable;

    // Comments assume employee/territory for illustration, but the code is generic

    // Get the collection of territories for this employee
    RelatedEnd entityCollection = (RelatedEnd)Column.EntityTypeProperty.GetValue(e.Entity, null);

    // In Edit mode, make sure it's loaded (doesn't make sense in Insert mode)
    if (Mode == DataBoundControlMode.Edit && !entityCollection.IsLoaded) {
        entityCollection.Load();
    }

    // Get an IList from it (i.e. the list of territories for the current employee)
    // REVIEW: we should be using EntityCollection directly, but EF doesn't have a
    // non generic type for it. They will add this in vnext
    IList entityList = ((IListSource)entityCollection).GetList();

    // Go through all the territories (not just those for this employee)
    foreach (object childEntity in childTable.GetQuery(e.Context)) {

        // Check if the employee currently has this territory
        bool isCurrentlyInList = entityList.Contains(childEntity);

        // Find the checkbox for this territory, which gives us the new state
        string pkString = childTable.GetPrimaryKeyString(childEntity);
        ListItem listItem = CheckBoxList1.Items.FindByValue(pkString);
        if (listItem == null)
            continue;

        // If the states differs, make the appropriate add/remove change
        if (listItem.Selected) {
            if (!isCurrentlyInList)
                entityList.Add(childEntity);
        }
        else {
            if (isCurrentlyInList)
                entityList.Remove(childEntity);
        }
    }
}

protected void gvMyGrid_DataBound(object sender, EventArgs e)
{




}

protected void CheckBoxList1_DataBound(object sender, EventArgs e) {
    MetaTable childTable = ChildrenColumn.ChildTable;

    // Comments assume employee/territory for illustration, but the code is generic

    IList entityList = null;
    ObjectContext objectContext = null;

    if (Mode == DataBoundControlMode.Edit) {
        object entity;
        ICustomTypeDescriptor rowDescriptor = Row as ICustomTypeDescriptor;
        if (rowDescriptor != null) {
            // Get the real entity from the wrapper
            entity = rowDescriptor.GetPropertyOwner(null);
        } else {
            entity = Row;
        }

        // Get the collection of territories for this employee and make sure it's loaded
        RelatedEnd entityCollection = Column.EntityTypeProperty.GetValue(entity, null) as RelatedEnd;
        if (entityCollection == null) {
            throw new InvalidOperationException(String.Format("The ManyToMany template does not support the collection type of the '{0}' column on the '{1}' table.", Column.Name, Table.Name));
        }
        if (!entityCollection.IsLoaded) {
            entityCollection.Load();
        }

        // Get an IList from it (i.e. the list of territories for the current employee)
        // REVIEW: we should be using EntityCollection directly, but EF doesn't have a
        // non generic type for it. They will add this in vnext
        entityList = ((IListSource)entityCollection).GetList();

        // Get the current ObjectContext
        // REVIEW: this is quite a dirty way of doing this. Look for better alternative
        ObjectQuery objectQuery = (ObjectQuery)entityCollection.GetType().GetMethod(
            "CreateSourceQuery").Invoke(entityCollection, null);
        objectContext = objectQuery.Context;
    }

    // Go through all the territories (not just those for this employee)
    foreach (object childEntity in childTable.GetQuery(objectContext)) {
        MetaTable actualTable = MetaTable.GetTable(childEntity.GetType());
        // Create a checkbox for it
        ListItem listItem = new ListItem(
            actualTable.GetDisplayString(childEntity),
            actualTable.GetPrimaryKeyString(childEntity));

        // Make it selected if the current employee has that territory
        if (Mode == DataBoundControlMode.Edit) {
            listItem.Selected = entityList.Contains(childEntity);
        }

        CheckBoxList1.Items.Add(listItem);
    }
}

public override Control DataControl {
    get {
        return gvMyGrid;
    }
}