Skip to content

{ Tag Archives } entity framework

Adding a [FixedLength] Attribute in Code-First Entity Framework

In Code First Entity Framework models, you can define the length of a string field with StringLengthAttribute, but you have to write code in OnModelCreating to indicate a CHAR/NCHAR fixed length field:

public class MyEntity
public int Id { get; set; }

public string FixedLengthColumn { get; set; }

public partial class MyContext : DbContext
public virtual DbSet MyEntities { [...]

Also tagged

Ad-hoc SQL/POCO Queries in Entity Framework 4.0

Since version 4.0, the Entity Framework has had the ability to query un-mapped data and project it onto POCOs using ad-hoc SQL.
Here, for example, is how we check the current SQL Server version:

internal class SqlVersionInfo

Also tagged ,

Sometimes, SELECT Really Is Broken

We got a bug report from a customer the other day; a certain query in one of our web services was giving the following error:
A column has been specified more than once in the order by list. Columns in the order by list must be unique.
Seems clear enough, except that

There was no duplication in [...]

Also tagged ,

Troubleshooting Entity Framework Connection Strings

In an application which uses the Entity Framework, you may see the following error at runtime:
MetadataException: Unable to load the specified metadata resource
The cause of this error is a missing or malformed Entity Framework connection string. In particular, the cause of this error is the metadata parameter of the connection string. Let’s examine this more closely.
Which Config File?
Before [...]

Also tagged ,

In LINQ, Don’t Use Count() When You Mean Any()

If you have a list, array, or query in a C#/LINQ application and need to check and see if the list is empty, the correct way to do this is to use the Any() extension method:

if (q.Any())

Similarly, you can check to see if any elements in the list meet a certain condition:

if (q.Any(i => i.IsSpecial))

If [...]

Also tagged ,

Don’t Depend Upon the ASP.NET Membership Tables

One very popular option for implementing user security in ASP.NET is to use Forms Authentication with the SQL Server membership provider. This provider creates several database tables to store user-related information, as well as a number stored procedures.
From time to time, a developer will attempt to add the ASP.NET Membership/Forms Authentication tables to their Entity [...]

Also tagged , , ,

Entity Framework Models and Source Control

As you’re probably aware, an Entity Framework model is stored in a single XML file, with the extension EDMX. Developers occasionally ask if this means that two people cannot work on the entity model concurrently. My answer to this is, "It depends." But I can give you some tips to make it easier.
Obviously, if you [...]

Also tagged , ,

join in LINQ to SQL and LINQ to Entities Considered Messy, Redundant

In this post I will demonstrate that use of the join keyword in LINQ to SQL and LINQ to Entities is nearly always wrong. LINQ queries which you write with the join keyword are harder to read and write than queries you write using associations, and they require knowledge of database metadata which is not [...]

Also tagged ,

Projecting Onto a Presentation Model with the Entity Framework and ASP.NET MVC

In this post, I will demonstrate how to map entity models to views in an ASP.NET MVC application without worrying about implementation details like eager loading, lazy loading, or having to manually optimize SQL for the task at hand. I will argue that expressing the relationship between an entity model in the presentation model in [...]

Also tagged ,

Interview With Me At

Jim McKeeth interviewed me for Episode 34 of The Podcast At
Share This | Email this page to a friend

Also tagged , ,

Bad Behavior has blocked 978 access attempts in the last 7 days.