.net - Global Entity Framework Context In WPF Application?
May 20, 2010
I am in the middle of development of a WPF application that is using Entity Framework (.NET 3.5). It accesses the entities in several places throughout. I am worried about consistency throughout the application in regard to the entities. Should I be instancing separate contexts in my different views, or should I (and is a a good way to do this) instance a single context that can be accessed globally?
For instance, my entity model has three sections, Shipments (with child packages and further child contents), Companies/Contacts (with child addresses and telephones), and disk specs. The Shipments and EditShipment views access the DiskSpecs, and the OptionsView manages the DiskSpecs (Create, Edit, Delete). If I edit a DiskSpec, I have to have something in the ShipmentsView to retrieve the latest specs if I have separate contexts right?
If it is safe to have one overall context from which the rest of the app retrieves it's objects, then I imagine that is the way to go. If so, where would that instance be put? I am using VB.NET, but I can translate from C# pretty good. I just don't want one of those applications where the user has to hit reload a dozen times in different parts of the app to get the new data.
All contexts are created in Using Blocks to dispose of them after they are no longer needed.When loaded, all entities are detatched from context before it is disposed.A new property in the MainViewModel (ContextUpdated) raises an event that all of the other ViewModels subscribe to which runs that ViewModels RefreshEntities method.After implementing this, I started getting errors saying that an entity can only be referenced by one ChangeTracker at a time. Since I could not figure out which context was still referencing the entity (shouldn't be any context right?) I cast the object as IEntityWithChangeTracker, and set SetChangeTracker to nothing (Null).
This has let to the current problem:When I Null the changeTracker on the Entity, and then attach it to a context, it loses it's changed state and does not get updated to the database. However if I do not null the change tracker, I can't attach. I have my own change tracking code, so that is not a problem.
My new question is, how are you supposed to do this. A good example Entity query and entity save code snipped would go a long way, cause I am beating my head in trying to get what I once thought was a simple transaction to work.
View 2 Replies
ADVERTISEMENT
Oct 15, 2011
I am trying to figure out Entity Framework but I keep running into issues with the context. I create my .edmx file and it works fine but when I try to declare my context on my pages Visual Studio will not find them. My .edmx file is called YCLModel.edmx and my connectionstring for it is YCLEntites.
I try to declare the context as:
Dim yclcontext as new YCLEntites
I have also tried going into design mode and dragging from the toolbox but when I select my named connection it gives me the following error:
The metadata specified in the connection string could not be loaded. Consider rebuilding the web project to build assemblies that may contain metadata. The following error(s) occurred: Unable to load the specified metadata resource.
View 1 Replies
May 13, 2010
c# - Are Microsoft Entity Framework and ADO.NET Entity Framework (.edmx) the same?
View 7 Replies
Jun 27, 2011
We are currently developing a WPF/Entity-Framework based application.One of our requirements was to allow the user option to select which environment(testing, training, or production) they will connect to when logging in.Each connection option points to its own database.In order to do this we are creating new EF contexts in the following way:[code]We have made sure that all new contexts are being disposed of once its job is completed.The problem is this: when the users are using the application we are seeing hundreds of SQL Server processes in SQL Server's activity monitor. Some individual users have upwards of 30+ processes associated when using the app. Once the user shuts down the app the associated processes all disappear - but while the app is running these processes seem to stick around.
View 1 Replies
Jul 22, 2009
I want to update an entity without loading the entity from the database first. I've accomplished this but only by knowing all of the entities properties and then using the "attachto" method. My issues is i don't want my app to need to remember all of the properties. Example:
[Code]....
View 3 Replies
Dec 14, 2011
Entity Splitting: one class, two or more tables.Here is how it is done in C#, but I need to get it working in vb.net.One more thing: the class name and table columns do not match, so I have to be able to map that out, too.I have to get this to work this way because the place I'm working at right now is a vb.net only shop and the database schema is fubar, but they have so much (millions) of lines of code done directly against the database in both asp classic, vb.net, AND asp.net webforms that changing the schema right now is not realistically possible.
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Post>()
.Map(m =>
[code]....
View 1 Replies
Jan 16, 2011
I am trying to jump from ASP Classic to asp.net. I have followed tutorials to get Entity Framework and LINQ to connect to my test database, but I am having difficulties figuring out ExecuteQuery(). I believe the problem is that I need an "entity class" for my database, but I can't figure out how to do it. Here is my simple code:
Dim db as New TestModel.TestEntity
Dim results AS IEnumerable(OF ???) = db.ExecuteQuery(Of ???)("Select * from Table1")
From the microsoft example site, they use an entity class called Customers, but I don't understand what that means.
View 1 Replies
Feb 18, 2010
My company is tossing around the idea of using the Entity Framework when it comes out with .NET 4. We are currently a VB.NET shop, but have some interest in switching to C#.Is there any major arguments for or against such a move?Does EF with C# hold any advantages in performance, coding ease, etc over VB.NET?
View 10 Replies
Apr 17, 2009
I am a newbie. I have been able to Add new entities where there is a One-To-Many Relation. I am having a problem (don't Know how to do it) adding a new Entity when the relation is using Many-To-Many. In my EDM I have:
[Code]....
View 1 Replies
Apr 16, 2011
I downloaded the VB 2010 Express software, the total files that we included in the download was 15, but it only loaded up 12 of these files. The following downloads did not get loaded as follows:MS Visual Studio 2010 ADO.NET Entity Framework Tools
MS HelpViewe 1.0 x64
I tried reinstalling the downloads that did not using the VB Express 2010 setup, but they do no appear on the list of downloads, only the SP1 for MS SQL Server 2008 Express Server (x64).Do I require the downloads that did not load.
View 2 Replies
Apr 21, 2012
Here are the entities that i have...
Public Class Account
Public Property AccountId As integer
Public Property AccountDescription As String
Public Property Transactions As List(Of Transaction)
End Class
[Code]...
i would like to make it suc that when i do "db.Account.find(1)" for example it also loads in the list of all transactions which have the coresponding AccountId. I'm not too sure what type of relationship this is?? anyway, right now i can do
Dim acct As Account = db.Account.Find(1) acct.Transactions = from ts in db.transactions select ts where ts.AccountId = acct.accountid but i know this is not the correct way, there must be a way to map this out so that entity can just load everything in one shot right?
View 1 Replies
Sep 28, 2011
I am used to using datasets for my projects in terms of data access. I now set myself an easy app to work on in order to learn the entity data framework and how to retrieve & manipulate data.Can anyone guide me through on where to start, what are the advantages of this model, In what scenarios should I choose to use entity framework and what are the things (basis) I should now before starting.My datasets are working fine too. Are there any reasons for me not to stick with that way of accessing data? Or is this something to consider depending on the users of the website (for speed purposes etc.)
View 3 Replies
Feb 28, 2012
I'm trying to learn EF in visual basic. I'm struggling with finding any easy to understand simple tutorial of how to do inner joins.
This is the SQL that i'm trying to replicate in EF:
SELECT Quote.LockedDateTime, IncommingQuoteStatus.StatusDesc, Users.FirstName
+ ' ' + Users.LastName AS UserName
FROM Quote
[Code].....
View 1 Replies
Feb 8, 2011
I have some basic expirience in Visual Basic and programming Windows Forms programs. I know now that forms programs are no longer updated by Microsoft and that WPF is the way to go. No to mention the slicker interface you can create. I've also noticed that passing SQL strings to a database is no longer the standard either. It's either Entity Framwork or Linq.
So I am going for a fresh approach. I need to develop a program that will access a sql database and be accessed by about 10 concurrent users within the office. What's the best approach in general? Linq or Entity Framework? Also, could you recommend a book or two for someone who is new to WPF and Entity Framework? I do have some programming expirience, just none with WPF or the new ways of ADO.net 4.
View 4 Replies
Nov 3, 2010
I'm currently testing the entity framework 4 for a simple app I wish to build.
how do you save and cancel changes on a record basis? Using the save changes method on the context persists all the changes to the database.
View 3 Replies
May 10, 2012
I'm trying to use the Entity Framework to create a complex query and get stuck in the joins. After browsing some of the documentations, I cannot seem to find the correct solution, so maybe someone here can help.
I have a data structure where a family has several family members, each of which can have more than one entry in the income table (on different registration dates) and can have more than one entry in the healthconditions table.
[Code]...
View 1 Replies
Jul 7, 2011
I need to make a if-else statement in the query meaning I only want to add the OpenAmount to ExpectedAmount if the type = Cash, the problem is that it only gives me the cash row not the other row.
[Code]...
View 1 Replies
May 14, 2012
I'm working on a project that was built using ADO.NET (raw sql) and Active Record pattern. I am slowly moving it away from ADO.NET to Entity Framework Code First 4.3.Here is an example of the pattern. The interface is a static Load and an instance Save (and Delete -- not shown).
Public Class Part
Public Property Id as Integer
Public Shared Function Load(_id As Integer) As Part
[code].....
View 1 Replies
Apr 6, 2011
I have the following code:
Private Sub btnReserve_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReserve.Click
Using dbContext As pbu_housingEntities = New pbu_housingEntities
[Code].....
executes it always get 0, because the row has not yet been created in the database. I can just move this last bit of code out into its own Context, but is there a better way to do this?
View 2 Replies
Dec 20, 2011
I am trying to make an MVC index view with bulk editing functionality, but can't seem to get it working. I have replaced the Html.DisplayFor with HTML.editorfor items, and put the whole table in a form which submits back to my index function. I have tried various ideas, but currently my function is
[Code]...
View 1 Replies
Jan 15, 2009
I am just starting out with ADO.net Entity Framework I have mapped two tables together and receive the following error:
Error 1 Error 11010: Association End 'OperatorAccess' is not mapped. E:Visual StudioprojectsBrandi IIBrandi IIHospitals.edmx 390 11 Brandi II
View 6 Replies
Dec 28, 2009
So, when you bind a collection of Entity objects to a grid component, the grid displays those fields in the sequential order they are found in the SQL Table they came from. This shows that the ordinal position of the fields are associated with their corresponding entity properties... somehow or other.
So here is the question: How can I obtain the table field ordinal position by reflecting over an Entity Framework Entity's properties? Let me tell you what I know and what I have tried. Apparently, each data field property in an EF Entity is decorated with a System.Runtime.Serialization.DataMemberAttribute. This attribute appears to have an Order property. However, I have discovered that this property does not contain what I am looking for. The value for all Data Properties in an Entity seems to be -1. What ever order is, it isn't ordinal position.
View 1 Replies
Nov 5, 2010
I noticed in the Entity Framework designer that you can map stored procedures for Insert, Update, and Delete operations. Is there any way to do this for Select operations as well, or is there a new direction for database access code where we don't really write stored procedures for our basic select operations any more?The company I work for is pretty adamant about always using stored procedures for every database operation, even though the Entity Framework makes the calls safe by calling sp_executesql.It seems like both LINQ to SQL and Entity Framework have moved away from using stored procedures for selecting the data. Is this an accurate statement?Just to clarify my question:I have a table in my database called Product. I use the wizard in the Entity Framework to generate my models...so I now have an Entity called Product. When I do the following query:
db.Products.SingleOrDefault(p => p.Id == 1);
It generates code similar to:
EXEC sp_executesql N'SELECT * FROM Product'[code].....
If it's not possible to do that using SingleOrDefault I'm fine with that. I'd much rather have the following:
db.Products.GetProduct(1);
Is this something that is normally done or do most people just have it dynamically generate the SQL?
View 2 Replies
Mar 12, 2011
I'm brand new to the Entity Framework and trying to learn all it can offer. I'm currently working my way through the MVC Music Store tutorial which includes the following code:
public ActionResult Browse(string genre) {
// Retrieve Genre and its Associated Albums from database
var genreModel = storeDB.Genres.Include("Albums")
.Single(g => g.Name == genre);
return View(genreModel);
[Code] .....
The problem is I'm getting the following exception:
Invalid column name 'GenreGenreId'.
Which I know is true, but I can't for the life of my work out where it's getting 'GenreGenreId' from.
Here is the source for my classes:
Album.vb
Public Class Album
Private _title As String
Private _genre As Genre
Private _AlbumId As Int32
[Code] .....
View 3 Replies
Mar 4, 2009
The question header is clear, i want to know why its different and why, when to select it for my project?
View 6 Replies
Aug 24, 2011
I have 2 entities that are related, but the legacy sql schema essentially has 2 key columns for the same table (not a 2-column key: see below). I need to create a relationship back to the 'faux key' column. Is there a way to do this declaratively in Entity Framework 4.1?
Public Class Client
Inherits ModelBase
<Key(), Required()>
Public Property ClientID As Decimal
<Required(), StringLength(50)>
Public Property ClientCode As String .....
And the error I am getting is:
One or more validation errors were detected during model generation: System.Data.Edm.EdmAssociationConstraint: : The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'ClientCode' on entity 'ClientLocation' does not match the type of property 'ClientID' on entity 'Client' in the referential constraint 'ClientLocation_Client'.
Because it thinks I'm trying to map ClientLocation.ClientCode > Client.ClientID, when I am really trying to map ClientLocation.ClientCode > Client.ClientCode...
View 4 Replies
Mar 30, 2009
I have a simple Entity Framework. For simplicity's sake I will transform the entity names to Northwind Like.I have a IEnumerable Of Customer and Item Objects
View 1 Replies
Apr 27, 2012
I'm trying to update a specific record based off of a users selection. Regarding Entity Framework syntax, I'm not very familiar. Is it possible to achieve this SQL statement in Entity FrameWork?
View 3 Replies
May 23, 2011
I have a large application that has always existed all in a single .NET project up until recently (200,000+ LOC). I'm finally getting to the point where I can fix this. I just started using the Entity Framework (4.1 using code first) and I believe this is a good time to start seperating my logic.
What I did is I separated my application into three subprojects:UI - Windows forms project Business - The POCO classes Data - Legacy data access code AND the DBContext class to connect my POCO classes to the database. I also have a Windows service project and will soon have a WCF project, but those concern me less.
The problem is that in this plan, the data layer depends on the business layer and vice versa causing a circular reference error. There is no way around this.So, my idea is to move my POCO classes into the data layer removing all business logic first (just keeping the database fields, basically). Then, I will inherit from those classes in the business layer adding back all the business rules. Then my data access layer will not rely on any other project, the business layer will rely just on the data access layer, and the interface layer will rely on both for now but eventually only on the business layer.
Does anyone see any problems with this, or a better way of doing things given my current circumstances?
View 5 Replies
Feb 23, 2009
I am trying to get connection to MySQL DB through the MS Entity Framework and I am not able to. Yes, I can connect to the DB otherwise, using conventional MySQL Connector 5.2.2. I am using vs 2008 and the VS SP1. I have added a VB Forms Project. Now, when I try to add a New ITEM and choose ADO.NET ENTITY DATA MODEL, it asks for the Datasource. Now, here it lists only SQL Server and I do not see the MySQL option at all. I have added the Mysql.data.dll to the project as a reference. Nothing seems to get me the link to the DB via the Entity Data Model Wizard...
View 2 Replies