简体   繁体   English

有没有办法通过 DbSet<T> 进入页面?

[英]Is there a way to pass the DbSet<T> into the page?

I have the window with the page.我有页面的窗口。 The page displays a data from the my database.该页面显示来自我的数据库的数据。 Also, on the window the buttons are placed, whose contents is according to the tables names of the database.此外,在窗口上放置按钮,其内容根据数据库的表名称。 These buttons switchs the page's content.这些按钮切换页面的内容。

For example, this is the btnUsers button's click event, which displays the "Users" table:例如,这是btnUsers按钮的点击事件,它显示“用户”表:

void btnUsers_Click(object sender, RoutedEventArgs e) {
    this.FrameMain.Navigate(new pageTable(Context.ctx.Users, ...));
}

The pageTable is the my "generic" page, that receives the Users class. pageTable是我的“通用”页面,它接收Users类。 Here is its constructor, which doesn't works:这是它的构造函数,它不起作用:

public pageTable(dynamic table, ...) {
    InitializeComponent();

    TableTemplate<dynamic>.Init(table, ...);
}

Here is the my generic class, that operates on the DbSet<T> :这是我的通用类,它在DbSet<T>

static class TableTemplate<T> {
    internal static void Init(T table) {
        foreach (string f in Foo(table, ...) {
            ...
        }
    }
}

The Foo method just extracts the columns from the DbSet<T> table: Foo方法只是从DbSet<T>表中提取列:

internal static string Foo<T>(T item, ...) {
    ...
}

The point is, that the application terminates when I try to get the data from the table, at the button's event, used this generic approach.关键是,当我尝试从表中获取数据时,应用程序终止,在按钮的事件中,使用了这种通用方法。

I noticed, that at the Foo method, during the debug, the T type is differs, depending on the way, with which I pass the DbSet<T> :我注意到,在Foo方法中,在调试期间, T类型是不同的,具体取决于我传递DbSet<T>

  • if I explicitly initialize a new class instance (for the test):如果我显式初始化一个新的类实例(用于测试):

     static class TableTemplate<T> { internal static void Init(T table) { foreach (string f in Foo(new Users(), ...) { ... } } }

    , then the T type is System.Data.Entity.DynamicProxies.Users_E006B3... , and the Foo method is works; ,那么T类型为System.Data.Entity.DynamicProxies.Users_E006B3...Foo方法有效;

  • without the explicitly initialization the T type is System.Data.Entity.DbSet`1[Namespace.Users] , and the Foo method isn't works.如果没有显式初始化, T类型是System.Data.Entity.DbSet`1[Namespace.Users] ,并且Foo方法不起作用。

Is there a possibility to pass the generic entity class into the page?是否有可能将通用实体类传递到页面中? I don't know if XAML has a generic classes support, to use the generic pageTable<T> page.我不知道 XAML 是否有通用类支持,以使用通用pageTable<T>页面。 It could be a solution, but I suppose there is a more neat way to pass the entity.这可能是一个解决方案,但我想有一种更简洁的方法来传递实体。

I found the solution, and it doesn't use the generic page.我找到了解决方案,它不使用通用页面。

Well, when I tried to print the table content, I discovered, that pass a structured query into the TableTemplate.Init() .好吧,当我尝试打印table内容时,我发现将结构化查询传递给TableTemplate.Init() I remember, that never used the dbContext.TableClass as a function argument before, always converting the TableClass into the list.我记得,以前从未使用dbContext.TableClass作为函数参数,总是将TableClass转换为列表。

I confess, I don't understand the EntityFramework and didn't expect such a result.我承认,我不了解 EntityFramework 并且没想到会出现这样的结果。 Generally, I used the object type and already forgot why used the dynamic type...一般我用的是object类型,已经忘记为什么用dynamic类型了...

I decided to pass the DbSet<T> , converted to the list (and this, as I understand it, are the different thigns) into the page's constructor, instead of the DbSet<T> .我决定将转换为列表的DbSet<T>传递给页面的构造函数,而不是DbSet<T> But, because I doesn't use the general page, I converted the items of this list into the object :但是,因为我不使用常规页面,所以我将此列表的项目转换为object

void btnUsers_Click(object sender, RoutedEventArgs e) {
    this.FrameMain.Navigate(new pageTable(Context.ctx.Users.ToList<object>()));
}

Now, the pageTable page's constructor is next:现在, pageTable页面的构造函数是下一个:

public pageTable(List<object> table) {
    InitializeComponent();

    TableTemplate<object>.Init(table, ...);
}

And, the Init method of the TableTemplate class is next:并且,接下来是TableTemplate类的Init方法:

internal static void Init(List<T> tableList) {
    if (tableList.Count > 0) {
        foreach (string f in Foo(tableList[0], ...) {
            ...
        }
    }
}

I haven't figured out how to display only the table's columns names, if tableList is empty.如果tableList为空,我还没有弄清楚如何只显示表的列名。 Thus, in this case, for now the page displays the empty DataGrid without the columns names.因此,在这种情况下,现在页面显示没有列名称的空DataGrid Nevertheless, I am glad, that could do I wanted.尽管如此,我很高兴,这可以做到我想要的。

At last, I will say, that indirectly agree with Xerillio .最后,我要说,间接同意Xerillio 的观点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM