Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Goal: To pass a list of related objects into a view which can then be iterated over using razor syntax.

I'm new to LINQ and MVC4. I'm trying to use the repository pattern to manage my model creation. I have set up a series of SQL database tables, and generated a DBML file using LINQ to SQL.

Now I seem to have two model style classes, one in the DBML file which was auto-generated.

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Projects")]
public partial class Project : INotifyPropertyChanging, INotifyPropertyChanged
{   
    private static PropertyChangingEventArgs emptyChangingEventArgs = ...
    private int _Id;    
    private string _Title;
    ...
}

And the other in a folder which i've manually created:

namespace Models
{
public class Project
{
    public Project(int ID, string Title, List<Group> Groups)
    {
        this.ID = ID;
        this.Title = Title;
        this.Groups = Groups;
    }
    public int ID { get; set; }
    public string Title { get; set; }
    public List<Group> Groups { get; set; }
}...

When I use attempt to get all projects through the repository it errors if I pass

return Context.GetTable<Models.Project>();

Which results in the following error:

Server Error in '/' Application.
The type 'Nimble.Models.Project' is not mapped as a Table.

Should I just bin my models, and use the models created by the LINQ to SQL?

share|improve this question
 
If GetTable() is to return your domain Project class then you will have to do some mapping inside that method that will perform the mapping from DataModel.Project to your DomainModel.Project objects –  dreza Mar 21 at 2:52

1 Answer

up vote 2 down vote accepted

If you are using MVC4 then you should use Entity Framework instead of Linq To Sql. And you solution does not work becouse you have created a completly different type then you have in your dbml model. Create a copy constructor in your extra Project class.

share|improve this answer
 
+1 especially for use Entity Framework. LINQ to SQL won't be getting any further attention from Microsoft, and with the Code First approach EF is much nicer to work with than previously. –  Yuck Mar 21 at 17:41
 
Thanks, i'll look into Entity Framework. So far it seems to be working well with my single set of models using EF, however i'm having difficulty with the one to many relationships.. but that's another question that i'll post if I get too stuck! :) –  Jack Mar 24 at 16:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.