Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code-First MetaData

What should I use as MetaData while I'm using Code-First Approach via EntityConnectionStringBuilder

EntityConnectionStringBuilder entityBuilder;
entityBuilder.MetaData = ??  // Metadata = @"res://*/;";

I got this error :

{"The specified metadata path is not valid. A valid path must be either an existing directory, an existing file with extension '.csdl', '.ssdl', or '.msl', or a URI that identifies an embedded resource."}
  • I didn't make any model on this approach, As I thought it isn't needed.

  • All I want is to do everything programmatically.

  • Everything was working fine in Database-First Approach.

  • Here I made the Connection String and passed it to the Context.

  • The EF Version is 5.0.

  • The database is existed.

  • Should I use any thing else to avoid MetaData Checking, such as initializers or ??

like image 592
LastBye Avatar asked Oct 30 '12 16:10

LastBye


People also ask

How do I convert code first to database-first?

There is no way to convert your code-first classes into database-first classes. Creating the model from the database will create a whole new set of classes, regardless of the presence of your code-first classes. However, you might not want to delete your code-first classes right away.

What is code first development?

In the ASP.NET MVC framework, the code-first approach is a development model where you first write the code that creates the data access layer, then you write the code that creates the controllers and views. In the code-first approach, you create a model, which is a class that represents the data in the application.

What is Entity Framework Code First?

It's an Entity Framework feature. Code First adds a model builder that inspects your classes that the context is managing, and then uses a set of rules or conventions to determine how those classes and the relationships describe a model, and how that model should map to your database.


1 Answers

For CodeFirst you use just a "regular" connection string - i.e. without metadata. CodeFirst will generate metadata artifacts for you under the cover and will pass them to the ObjectContext instance without any additional action required. You should not use EntityConnectionStringBuilder.

Typically you just derive your context from the DbContext and make a parameterless constructor that calls base and passes the name of the connection string from the config e.g.:

public class MyContext : DbContext
{
    public MyContext() : base("Name=NorthwindConnectionString") {}
}

You can also pass a connection string to the DbContext. If you would like to use a connection string builder you would use the SqlConnectionStringBuilder for this and not the EntityConnectionStringBuilder.

like image 99
Pawel Avatar answered Sep 29 '22 07:09

Pawel