7

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 ??

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Pawel, I found the answer and posted. I asked for the MetaData replacement in CF, not the ConnectionString ;), thanks participating.
0

You should add the connection string details as well as the paths to the files with the generated model, below is an example of what you should put into it.

string efConnectionString= @"metadata=res://*/Blogging.csdl|res://*/Blogging.ssdl|res://*/Blogging.msl;provider=System.Data.SqlClient;provider connection string=""Data Source=.\SQLEXPRESS;Initial Catalog=Blogging;Integrated Security=True;MultipleActiveResultSets=True""";

EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder(connectionString);

SqlConnectionStringBuilder sbuilder = new SqlConnectionStringBuilder(builder.ProviderConnectionString);

sbuilder.DataSource = "New Datasource";

builder.ProviderConnectionString = sbuilder.ConnectionString;
efConnectionString = builder.ConnectionString;

1 Comment

As I said I did this when using Database-First, In Code-First I didn't make any model, and If I specify these names I will get another error cause of not existing models !
-2

The Answer is simply this : Instead of MetaData what is missed is the Model Mapping and it should be done here :

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // The Mapping
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.