I have five table in Sql-Server, and which have linked using foreign keys.
Below i am displaying the image of database tables and links.
For example i tried to fetch user data of a particular user ID from user table, i am getting all rows with inner join to all five tables.
But i just need data from only User table. I tried to write query , sql procedure and also Linq.
In all i am getting the data which is joined with all five tables.
How can i achieve the data from only one table ?
My Try using Procedure ,
object[] xparams = {
new SqlParameter("@UserName", "Admin"),
new SqlParameter("@UserPwd", "1234"),
new SqlParameter("@AppType", 1)
};
var user = db.Database.ExecuteSqlCommand("exec proc_authenticate_tocken @UserName, @UserPwd, @AppType ", xparams);
Another using Query ,
db.Users.SqlQuery("select * from users where PhoneNumber ='"+ user_name + "' and Password = '" + password + "' and TypeId=" + user_type + " and Status = 1;").ToList();
Lsing Linq,
var ty = db.Users.Where(emp => emp.Password == password &&
emp.PhoneNumber == phone_number)
;
Code below is Entity Model Class, auto generated using Entity Framework.
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.TockenPools = new HashSet<TockenPool>();
this.TockenProcesses = new HashSet<TockenProcess>();
}
public int Id { get; set; }
public Nullable<int> TypeId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public string PhoneNumber { get; set; }
public string ActualName { get; set; }
public Nullable<System.DateTime> LastLogin { get; set; }
public Nullable<int> LoginCount { get; set; }
public Nullable<int> Status { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TockenPool> TockenPools { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<TockenProcess> TockenProcesses { get; set; }
public virtual User Users1 { get; set; }
public virtual User User1 { get; set; }
public virtual UserType UserType { get; set; }
}