简体   繁体   中英

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:

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. 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> :

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:

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> :

  • 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;

  • without the explicitly initialization the T type is System.Data.Entity.DbSet`1[Namespace.Users] , and the Foo method isn't works.

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. 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() . I remember, that never used the dbContext.TableClass as a function argument before, always converting the TableClass into the list.

I confess, I don't understand the EntityFramework and didn't expect such a result. Generally, I used the object type and already forgot why used the dynamic type...

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> . But, because I doesn't use the general page, I converted the items of this list into the 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:

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

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

And, the Init method of the TableTemplate class is next:

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. Thus, in this case, for now the page displays the empty DataGrid without the columns names. Nevertheless, I am glad, that could do I wanted.

At last, I will say, that indirectly agree with Xerillio .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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