Background
I am starting a series of simple screens to display and update info in our ERP database.
I have worked through the wpf controls and understand the need for Observable Collections and after reading around on Entity Framework I understand the advantages of it sitting on top of ADO.net compared to the basic SQL methods and Datatables I am more comfortable with due to my SQL experience.
When I tried EF when I was first started working with Data CRUD screens I struggled to get the Observable Collections I needed, but having read this walk through last night ( https://msdn.microsoft.com/en-us/data/jj574514.aspx) and seen the notes for VS 2010 to edit EF code to get Observable Collections I think I want to try EF again.
Question
My Data screen needs present information which has be combined from five tables and a couple of sub views to be meaningful to the user.
Included in the dataview is a simple Y/N flag which comes from one of the five Datatables.
Can the user update the Y/N flag through the view mapped to EF, displayed in WPF datagrid ?
Or do I have to map all the base Datatables and sub views and recreate the view and Data Context in EF to allow the update to work?
If it is the latter does any one know of any tutorials or walk through I can use on my test development to try EF please
Thanks
The user can update a field via the view mapped to EF, however it is a little bit more complicated.
For a single table mapped to EF, the update is done by EF automatically, for a view mapped to EF you need to define the update function in the mapping details.
The function would be in form of an SQL stored procedure mapped to EF.
Related
I'm in the process of creating a piece of software that processes data on an existing SQL Server database (which I can't change).
I'm using visual studio to develop my application in C# using WPF and the MVVM framework.
Essentially, the main purpose of my program is to gather data from the several tables of the database and present it to the user in a meaningful way. I don't want to simply display the data in the tables, but instead to gather information spread over several tables and aggregate in a certain way.
For that purpose, I've already created several Views in the database (using SMSS), some of which are rather complicated.
My question is: should I map those Views in Entity Framework and use the created POCO class as the source of a Datagrid, or should I recreate those Views through a LINQ query? Secondly, can I use a list of anonymous type as the source of a datagrid, taking into account that it would, naturally, be read-only?
Thanks
First, if your views have no performance issues, you should map views to POCO. You do not need to recreate the wheels.
Secondly, you can bind a collection of anonymous type to source of a DataGrid, but anonymous type binding has a limitation of automatic column generation.
I'm having a hard time getting Entity Framework to do what I want it to do. I've got a form generator application that's already up and fully running in .NET WebForms. I've just begun the process of converting this over to use .NET MvC instead, with React as the view layer and Entity Framework to drive the database stuff.
One part of this application is intended to allow the users to add form controls to a list and have them laid out visually as they would be shown on the final published form. So you click on First Name, and it adds an input control with ID FirstName, header text above it, etc.
I've got the view layer set up to show some basic info about the controls and put them into a list. I've also already got all the database tables with the assembled information about various types of controls you can add to the page. My WebForms version of this manages all the relationships between various data tables and how that is packaged for the view layer through code.
I've been trying to use my existing tables in Entity Framework but haven't been able to get any of the foreign key relationships to work correctly. When I add them in MS SQL, they don't carry over and give me ways of accessing the data from the foreign tables via the objects representing a main table.
When I just leave the tables without any foreign key relationships in SQL itself
and add them to the model in Visual Studio, it also won't sync up the objects correctly. It will throw errors relating to missing mapping or that it can't find the data in the foreign objects.
So I've reached the point where I am importing all my tables, which does work correctly, but then I have to update the model for each table to add an object or collection of objects that can contain a reference to the corresponding matched foreign key data. Then I have to write procedures which will go through and set the object references themselves according to ID columns which already exist in the database (ControlType in Controls table matches up to ID in ControlTypes table, etc.).
Then if I make a change in the database and update the model, it will remove all the custom objects I've added to the model, and possibly throw more errors to boot if I've removed a column on the database. What a headache!
So I know that seems like a wall of text, but I'm at a loss for what to do. I'd like to just have a big data object that I can pass around for each control that contains all the related info pertinent to that particular control and can be used by React in my view layer to display the info.
An alternative to that is I could build in Ajax requests into the various components which would make database calls to get the info relevant to that particular portion of the control, but that seems like a huge pain.
Am I going about this all wrong or is EF really just this hard to use?
I have a table that used throughout an app by Entity. I have a view that returns an identical column set, but is actually a union on itself to try to work around some bad normalization (The app is large and partially out of my hands, this part is unavoidable).
Is it possible to have Entity 4 treat a view that is exactly like a table as the same type, so that I can use this view to populate a collection of the same type? This question seems to indicate it is possible in nhibernatem but I can't find anything like it for entity. It would be an extra bonus of the navigation properties could still be used to Include(), but this is not necessary (I can always manually join).
Since EF works on mappings from objects to database entities this is not directly possible. What you need is something like changing the queried database entity dynamically, and AFAIK this is not possible without manually changing the object context.
For sure the EF runtime won't care as long as it can treat the view as if it was completely separate table. The two possible challenges that I forsee are:
Tooling: Our wizard does allow you to select views when doing reverse engineering (i.e. database-first). Definitively if you can use 'code first against an existing database' you can just pretend that the view is just a table, but you won't get any help scripting the database creation or migrations.
Updates: in general you can perform updates for a view setting up store procedure mapping (which is available in the EF Designer from v1 or in Code First starting in EF6). You might also be able to make your view updatable directly or using instead off triggers (see "Updatable Views" here for more details). If I remember correctly the SQL generated by EF to retrieve database generated values (e.g. for identity columns) is not compatible in some cases with instead-off triggers. Yet another alternative is to have your application treat the view as read-only and perform all updates through the actual table, which you would map as a separate entity. Keep in in mind that in-memory entities for the view and the original table will not be kept in sync.
Hope this helps!
I am facing one problem. I am working on a project which has requirement of dynamically populating Grid control to add, update and remove records of specific table.
database is not finalized yet. so what i want is, if i add new column to a table and run the application. that grid should contain newly added column so that i can add new row. update or delete existing row.
I have crated DAL using LINQ to SQL but that is not covering my requirement. I want
Get name of tables from database and show them in dropdown list.
after selecting table name. grid should populate with all the columns. so that i can add/update/delete records.
So what exactly is your problem, you want to know how to get the table list from the database? If it is so, and if you're using SQL Server, you could run a select like that :
select name from sysobjects where xtype = 'U'
You can explore this system tables : sysobjects and syscolumns, they store the metadata information on the database.
From what I can gather, your best bet would be taking a different approach than Linq to SQL. You are looking for a UI which directly reflects your domain and can be generated automatically / dynamically. Two methods come to mind:
You can leverage MS Dynamic-Data which is an ASP.NET WebForms-based technology. You wire it up directly to a database or Entity Framework model. It generates the grids for all CRUD operations. It detects relationships via foreign keys and can generate the tables with links to one another. It's very customizable.
Dynamic Data
There is another architectural pattern called "Naked Objects". This requires rich, well-designed domain and aggregate roots. The UI should be 100% generated from this domain model. See the videos on this site to get a great example.
One example I can give you is, recently, our team has been divided - some working on an SOA application which integrates with our main product. Our developer resources are all focused on the task at hand writing WCF services, architecture, database engineering, ASP.NET, etc etc. We needed an internal application which we could use to administer the new SOA application. We could not dedicate another group of guys to build out a new application.
By using Dynamic Data, we had the entire administration app up and running off our EF 4 model in no time. It's doing everything we needed and minimal resources were exerted.
Use ADO.NET Entity Framework for your DAL than using LINQ TO SQL.
Well I solved this problem by getting table info from database schema.
build table to grid on run time which auto generates all columns.
created insert/update/delete query on fly by getting columns name from grid column name.
Happy coding:)
can microsoft sync framework sync database views ?
or in your SelectIncrementalInsertsCommand can you select and join to multiple tables kinda doing something like a view?
Sync Framework does not support Views. You can changes the SelectIncrementalInsertsCommand to return data, however you need to make sure it gives back exact schema expected, no extra columns no mis matched data types. There is another way if your scenario really demands extra info. You can manipualate the data set retrieved in the ChangesSelected event of Provider. However you need to be careful about performance hit here.
Views are created dynamically on execution s you cant sync them.
BUT you can:
aggregate the view's data into a table and then sync the table.
Run the view on the synced tables on the target DB.