1

I come from the web forms world, and I'm learning MVC4 and Entity Framework 5 with a code-first approach. This is my first time using EF code-first and I'm having some trouble understanding what EF needs from me to be able to create/update a database.

I'm making a request to a RESTful service and deserializing the response into a DataContract. Ideally, I would like to be able to insert this DataContract into a database but I'm unsure how to generate the DbContext classes from the DataContract so EF can do it's thing.

The simplified version of the DataContract...

[DataContract]
public class MoviesDataContract {
    [DataMember(Name = "total")]
    public int Total { get; set; }

    [DataMember(Name = "movies")]
    public IEnumerable<Movie> Movies { get; set; }

    [DataMember(Name = "link_template")]
    public string LinkTemplate { get; set; }
}

[DataContract]
public class Movie {
    [DataMember(Name = "id")]
    public int Id { get; set; }

    [DataMember(Name = "title")]
    public string Title { get; set; }

    [IgnoreDataMember]
    public int? Year {
        get {
            int i;
            return int.TryParse(_year, out i) ? i : (int?)null;
        } 
        set { _year = value.ToString(); }
    }

    [DataMember(Name = "year")]
    private string _year { get; set; }
}

Here's an example of the DataContract usage...

using(var response = (HttpWebResponse)request.GetResponse()) {
    var serializer = new DataContractJsonSerializer(typeof (MoviesDataContract));
    var dataContract = (MoviesDataContract)serializer.ReadObject(response.GetResponseStream());

    var model = new List<MovieViewModel>();
    dataContract.Movies.ToList().ForEach(movie => model.Add(new MovieViewModel {
        Title = movie.Title,
        Year = movie.Year
    }));

    // MovieDbContext doesn't exist and it's what I'm unsure of how to generate
    // I would think it would work something like this though (once created)
    var db = new MovieDbContext();
    db.Add(dataContext);
    db.SaveChanges();

    return View(model);
}

My Questions are...

  1. Is there a way to generate the DbContext classes from my DataContract or do I need to manually create them?
  2. Is there a better approach to using (or really saving to a db) Deserialized data with Entity Framework (especially EF5)?
  3. Could someone provide a link that discusses EF (4 or higher) being used in conjunction with a DataContract? I think I may not have a complete grasp on how to effectively use EF5.

1 Answer 1

3

Have you tried feeding your data contracts to DBSet's like you would when starting a normal code-first project?

public class ProductContext : DbContext

{

    public DbSet<Category> Categories { get; set; }

    public DbSet<Product> Products { get; set; }

}

EF should ignore the existing attributes and create some entities.

See this article for a good example of how to work with DataContext: http://msdn.microsoft.com/en-us/jj729737

Since you're new to EF you might as well start here: http://msdn.microsoft.com/en-us/ee712907

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

1 Comment

This is perfect, thank you for the links, they're exactly what I'm looking for :)

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.