entity-framework

WPF: "Items collection must be empty before using ItemsSource."

I'm trying to get images to display in a WPF ListView styled like a WrapPanel as described in this old ATC Avalon Team article: How to Create a Custom View. When I try to populate the ListView with a LINQ-to-Entities queried collection of ADO.NET Entity Framework objects I get the following exception: Exception Items collection m...

How to avoid hardcoded field names in entity framework?

How do i avoid using hardcoded table and field names in entity framework? For example: Contact contact = contactQuery.Include("SalesOrderHeader.SalesOrderDetail") .FirstOrDefault(); Is there a way to get that info from the context or metadata? I'd love to be able say SalesOrderHeader.TableName or SalesOrderHeaderFiel...

Can Entity Framework connection strings reuse an existing connection string?

In my configuration files I have a connection string used by a legacy part of the app (using Datasets) and another string for Entity Framework: <connectionStrings> <add name="Database" connectionString="Server=..." /> <add name="Entities" connectionString="metadata=.....connection string='Server=..." /> </connectionStrings> Th...

Casting Entity Framework Entities in the "Wrong" Direction

I am using the Entity Framework and have an inheritance structure with a base Entity (let's call it Customer) and a derived Entity, let's call it AccountCustomer. The difference is that an AccountCustomer has extra details (such as payment terms etc.) stored in a separate table in the database and therefore extra properties in the Entity...

ADO.NET Entity Framework - LINQ to multiple dbms

Is it possible to create an 'Entity' that will be an abstraction of a relationship between tables that live in two different tables, in two different databases, on two different machines, and even possibly using two different dbms? For example, if I have a SQL Server db on one machine that stores all my customers, and I have an Oracle d...

Basic SQL / Entity Framework 1 to 0..1 entity model question

In my database I have 2 tables A and B A has columns ID, Title, and B_ID. B_ID allows nulls. B has columns ID and SomeText ID in both is primary key identity A also has a unique index on B_ID This seems like a basic way to say that A can have 0 or 1 B rows associated with it, and each B is associated with exactly 1 A. Is that correct s...

Why is Entity Framework taking 30 seconds to load records when the generated query only takes 1/2 of a second?

The executeTime below is 30 seconds the first time, and 25 seconds the next time I execute the same set of code. When watching in SQL Profiler, I immediately see a login, then it just sits there for about 30 seconds. Then as soon as the select statement is run, the app finishes the ToList command. When I run the generated query from M...

Need help with ADO.NET Entity Framework UpdateException.

I have in my Database a Contact table and a Phone table, in the phone table there is column "ContactId" with a key that references the phone to the contact. In the key options in the server I set its delete option to 'Cascade'. Now when I attempt to delete a contact using the context: Dim contact As Contact 'The contact I want to dele...

Entity Framework: Detect DBSchema for licensing

We're working on a product that may or may not have differing license schema for different databases. In particular, a lower-tier product would run on SQLExpress, but we don't want the end user to be able to use a "full-fat" SQL install - benefiting from the price cut. Clearly this must also be the case for other DBs, so Oracle may comma...

MetadataException: Unable to load the specified metadata resource

All of a sudden I keep getting a MetadataException on instantiating my generated ObjectContext class. The connectionstring in App.Config looks correct - hasn't changed since last it worked - and I've tried regenerating a new model (edmx-file) from the underlying database with no change. Anyone have any... ideas? Edit: I haven't changed...

How to catching file open events

Is there a class in the .NET framework that I can used to catch files being opened? FileSystemWatcher only fires when a file is altered. ...

Updating your edmx to reflect changes made in your db (.net linq-to-entities)

So I have my edmx made. Then I change my database a little bit, changing a column to from being a NOT NULL to allowing NULL. I go into my edmx, right click and choose "Update Model from Database" Now I go into my program and it hasnt actually updated... I can't put a null in the column. What do I have to do to update the edmx prope...

How to easily convert a DbDataReader object into something useful?

I am manually calling a stored procedure using an Entity Framework EntityConnection like so: DbConnection storeConnection = entityConnection.StoreConnection; DbCommand command = storeConnection.CreateCommand(); command.CommandText = "sp_GetMyComplexData"; command.CommandType = CommandType.StoredProcedure; DbDataReader reader = command....

How do I properly load Object References with ADO.net Entity Framework

What I'm doing works, but it's just wrong, I know it. public Profile GetProfile(int id) { Profile profile = ( from u in en.User where u.UserID == id select u.Profile.FirstOrDefault() ).First(); if (profile.UserReference.Value == null) profile.UserReference.Loa...

How to generate Entity Framework or Linq To SQL wrappers for system stored procedures?

Title says it all. I want to be able to work with system stored procedures (sp_helpXXX etc) through generated Linq-To-SQL or Entity Framework wrappers. Problem is, system sprocs are not listed in the generation wizards. I have also tried running sqlmetal.exe manually, but no system stored procedures show up. ...

Setting the default value of a DateTime Property to DateTime.Now inside the System.ComponentModel Default Value Attrbute

Does any one know how I can specify the Default value for a DateTime property using the System.ComponentModel DefaultValue Attribute? for example I try this: [DefaultValue(typeof(DateTime),DateTime.Now.ToString("yyyy-MM-dd"))] public DateTime DateCreated { get; set; } And it expects the value to be a constant expression. This is in ...

How can I refactor these 2 methods into 1?

I am trying to refactor some Entity Framework code where I have a Products entity and related entities for the Product's series, family, brand, etc. The series, family, and brand entities all have the same basic structure with an Id and Text property. The two methods below are very similar and should be able to be refactored to a sin...

Is it possible to update a sql database schema from an edmx in Visual Studio 2008?

So with LINQ-to-SQL I know you can update the database from the LINQ objects and update the LINQ objects from the SQL. I am using LINQ's entity framework (a.k.a. LINQ-to-entities) and I can update the entities from the SQL database, but I can't update the database schema by changing the entities. This is frustrating. Is there somethin...

How do I save an entity for the first time that requires another pre-existing entity to be attached to it

I need to save a Company entity to the database as a new entity. In order to do this I need to associate a CompanyType entity to the Company entity. I can't save the Company without do this. Do I attach the newly created company to the context and then attach the company type entity to the company and then save my results? This code ...

Entity framework and foreign keys

Hi, I have two tables a car tables which contains different properties for a car, and a carmodeldefinition which is a foreign key to a table with the same name. The cardefinition table contains the different cars and models. I have mapped them in the entity framework http://img21.imageshack.us/my.php?image=entityframeworkfk.jpg (can't g...