简体   繁体   中英

ADO.NET MVC Query foreign key

I have two models connected with a foreign key, User and Farm.

I want to be able to select a user with a query and type:

@Model.Farm.FarmId

In my view. This wont work because the farm prop is null.

空值

These are my two models:

Model 1:

public class User
{
    [Key]
    public int UserId { get; set; }  

    public string UserName { get; set; }
    public string Password { get; set; }
    public string PasswordSalt { get; set; }
    public int Money { get; set; }

    public int FarmId { get; set; }

    [ForeignKey("FarmId")]
    public virtual Farm Farm { get; set; }
}

Model 2:

public class Farm
{
    public Farm()
    {
            User = new HashSet<User>();
    }

    [Key]
    public int FarmId { get; set; }

    public DateTime Created { get; set; }

    public int Age { get; set; }

    public virtual ICollection<User> User { get; set; }
}

GET USER Query:

public User GetUser(string userName)
{
    string sql = "SELECT * FROM Users WHERE UserName = @UserName";

    User user = null;

    using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    {
        try
        {
            SqlCommand cmd = new SqlCommand(sql, connection);
            cmd.Parameters.AddWithValue("@userName", userName);

            connection.Open();

            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                user = new User
                {
                    UserId = (int)rdr["UserId"],
                    UserName = (string)rdr["UserName"],
                    Password = (string)rdr["Password"],
                    PasswordSalt = (string)rdr["PasswordSalt"],
                    Money = (int)rdr["Money"],
                    FarmId = (int)rdr["FarmId"],

                    Farm ??????
                };
            }

            if (rdr != null)
                rdr.Close();
        }
        catch
        {

        }
    }

    return user;
}

My guess is I have to do some kind of join, right? I'm still a query-newbie so have patience with me please:)

I dont want to use linq!

Thanks!

If you want to use plain SqlConnection , ditch Entity Framework altogether. It's not going to help you. The metadata you've added to your objects will go unused.

Even with Entity Framework you can theoretically use DbSet.SqlQuery or a similar method to run custom SQL queries and get back live objects. I don't think there's a documented way to eager load dependencies via a JOIN though. You'll end up with lazy loading support only, ie SELECT queries in a loop when you enumerate things.

If you absolutely must use your convoluted manual way instead of a simple Linq one-liner, then what you want is a simple INNER JOIN (or LEFT JOIN if the Farm is optional). That way you will get all the fields from the Farm table on each row as well. You can then manually instantiate your Farm object for each User the same way you're doing with the rest of the fields.

ProTips:

  1. Use using() with the reader to ensure it gets closed properly instead of your if
  2. An empty catch { } is not going to help you debug things.
  3. Why use a while loop if you're only expecting one result?

Add a join to your SQL query as below

string sql = "SELECT * FROM Users JOIN Farm ON Farm.FarmId = Users.FarmId WHERE UserName = @UserName";

then create an instance of Farm and assign it to user.Farm inside the while (rdr.Read()) block

while (rdr.Read())
{
    user = new User
    {
        UserId = (int)rdr["UserId"],
        UserName = (string)rdr["UserName"],
        Password = (string)rdr["Password"],
        PasswordSalt = (string)rdr["PasswordSalt"],
        Money = (int)rdr["Money"],
        FarmId = (int)rdr["FarmId"]
    };

    Farm farm = new Farm();
    farm.FarmId = (int)rdr["FarmId"];
    farm.Created = (DateTime)rdr["Created"];
    farm.Age = (int)rdr["Age"];

    user.Farm = farm;
}

If you don't want to use EntityFramework, you must build Farm object yourself.

Try join user and farm to get farm object that connected to user:

select * from users u
join farms f on u.farmid = f.farmid
where username == @username

And then build Farm object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM