1

I create a project where I use EF with LINQ and Model first. So, based on my edmx I created my Database and also my classes.

So I got some problems. I created a Click to test if my code is working.

protected void btnSearch_Click(object sender, EventArgs e)
{
    ZUser Zusr = new ZUser();
    List<ZUser> lst = Zusr.ListAll();
    // Zusr.Id = 1;

    string test = "";
    foreach (ZUser item in lst)
    {
        test = item.Name;
    }
    lblName.Text = test;
}

So in my ZUser Class (Controller) I did the following code:

[Serializable]
public class ZUser : User
{
    String connString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();

    public List<ZUser> ListAll()
    {
        List<ZUser> lstUser = new List<ZUser>();
        using (DataContext db = new DataContext(connString))
        {
            Table<User> Users = db.GetTable<User>();
            var query =
                from usr in Users
                where usr.Name == "Test"
                select usr;

            foreach (ZUser usr in query)
                lstUser.Add(usr);
        }

        return lstUser;
    }
}

And my Model (Class generated by my edmx)

namespace System.Model
{
    //[Table]
    public partial class User
    {
        public int Codigo { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
        public string Password { get; set; }
        public DateTime Created { get; set; }
        public DateTime LastLogin { get; set; }
    }
}

Problems

  1. If I don't let the [Table] in my Model class (I added that) I got this error. I'm not sure if this is the right way to correct it.

The type '{0}' is not mapped as a Table.

  1. After "fixing" the problem from above. I got this new one in my foreach (ZUser usr in query).

The member '{0}.{1}' has no supported translation to SQL.

I don't know how to fix or create a workaround this one.

Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
  • Sorry, I don't want to be rude, but the way you mix domain, transport and data responsibilities is very smelly. `ZUser` should not derive from `User` and it should either be part of the data layer _or_ be a DTO/viewmodel (for data transfer), not both. If you leave it this way your issues will be beyond repair. – Gert Arnold Feb 20 '13 at 18:56
  • @GertArnold thanks for point that out. Could you point me to the right direction here? I was trying to create a Controller with ZUser that inherit from User (model class generated by edmx). So I could call ZUser to do the things I need (like `Selects`, `Inserts`...) from my view page (**.cs**). – Michel Ayres Feb 20 '13 at 19:18

1 Answers1

1

Amazing, this feature of linq!

Really intresting!

After some searches on msdn and test it in application,

maybe you miss the Column attribute over all single class members:

[Column(CanBeNull = false, DbType = "int")]

And maybe you must uncomment the Table attribute on top of User declaration

Hope this help!

T-moty
  • 2,679
  • 1
  • 26
  • 31