Domanda

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.

È stato utile?

Soluzione

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!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top