使用Entity Framework从多个表中检索数据

时间:2018-11-20 21:26:55

标签: c# .net entity-framework

我想基于每个表的<?php session_start(); $_SESSION['message'] = ''; $mysqli = new mysqli('serverip', 'trysil', 'mypass123', 'trysil'); if ($_SERVER['REQUEST_METHOD'] == 'POST') { //Emails are matching if ($_POST['email'] == $_POST['email2']) { $firstname = $mysqli->real_escape_string($_POST['firstname']); $lastname = $mysqli->real_escape_string($_POST['lastname']); $personalnumber = md5($_POST['personalnumber']); //Hash:a Personnummer $gender = $mysqli->real_escape_string($_POST['gender']); $email = $mysqli->real_escape_string($_POST['email']); $phone = $mysqli->real_escape_string($_POST['phone']); $adress = $mysqli->real_escape_string($_POST['adress']); $_SESSION['firstname'] = $firstname; $_SESSION['lastname'] = $lastname; $_SESSION['personalnumber'] = $personalnumber; $_SESSION['email'] = $email; $_SESSION['phone'] = $phone; $sql = "INSERT INTO anmalningar ('firstname','lastname', 'personalnumber', 'email', 'phone', 'gender') VALUES ('$firstname', '$lastname', '$personalnumber', '$email', '$phone', '$gender')"; if ($mysqli->query($sql) === true) { $_SESSION['message'] = "Det lyckades"; header("location: welcome.php"); } else { $_SESSION['message'] = "Could not send form, send error"; } } else { $_SESSION['message'] = "Emails don't match"; } } else { $_SESSION['message'] = "Not POST"; } ?> <!DOCTYPE html> <html lang="en" dir="ltr"> <body> <div class=""> <?= $_SESSION['message'] ?> </div> <form class="" method="post"> <div style="border-bottom: 2px solid black;" class="main-info"> <h3 style="font-size:2vw;">Allmän Info</h3> <div class="firstname"> <h4>Förnamn</h4> <input type="text" name="firstname" value="" required> </div> <div class="lastname"> <h4>Efternamn</h4> <input type="text" name="lastname" value="" required> </div> <div class="personalnumber"> <h4>Personnummer</h4> <input type="text" name="personalnumber" value=""> </div> <div class="gender"> <h4>Kön</h4> <input type="radio" id="man" name="gender" value=""> <label for="man">Man</label><br> <input type="radio" name="gender" id="female" value=""> <label for="female">Kvinna</label> </div> <div class="email"> <h4>E-Post</h4> <input type="text" name="email" value=""><br> <h5>Verifiera E-Post</h5> <input type="text" name="email2" value=""> </div> <div class="phone"> <h4>Telefon</h4> <input style="margin-bottom:15px;" type="text" name="phone" value=""> </div> </div> <br> <br> <div style="border-bottom: 2px solid black;"class="adress"> <h3 style="font-size:2vw;">Adressinformation</h3> <h4>Gata och Husnummer</h4> <input type="text" name="adress" value=""> <h4>Postkod</h4> <input type="text" name="postalcode" value=""> <h4>Postort</h4> <input style="margin-bottom:15px" type="text" name="postalcity" value=""> </div> <br> <br> <div style="border-bottom: 2px solid black;" class="parent-info"> <h3 style="font-size:2vw;">Kontaktinformation, Vårdnadshavare</h3> <h4>Vårdnadshaves namn</h4> <input type="text" name="nameparent" value=""> <h4>Vårdnadshaves E-Post</h4> <input type="text" name="emailparent" value=""> <h4>Vårdnadshaves telefonnummer</h4> <input style="margin-bottom:15px;" type="text" name="phoneparent" value=""> </div> <br> <br> <div class="other"> <h3 style="font-size:2vw;">Övrig info</h3> <h4>Allergier</h4> <input type="text" name="allergies" value=""> <h4>Övrigt</h4> <input type="text" name="other" value=""> </div> <input type="submit" name="submit" value="Skicka"> </form> </body> </html> 从多个表中获取数据。

目前,我有一个项目表,其中一个itemId列与另一个称为ItemId的表具有1:m的关系:

ExplicitMods

我还为两个表设置了上下文:

namespace Poe.Models
{
    public class Item
    {
        [Key]
        public string ItemId { get; set; }
        public List<ExplicitMod> ExplicitMods { get; set; }
    }
}

namespace Poe.Models
{
    public class ExplicitMod       
    {
        public ExplicitMod(string Name)
        {
            this.Name = Name;
        }

        [Key]
        public string ItemId { get; set; } 
        public string Name { get; set; }
    }
}

然后我尝试调用项目表,搜索名为“ Brood Star”的随机名称,然后加入explicitMod表:

namespace Poe.DataAccess
{
    public class DatabaseContext : DbContext
    {
        public DbSet<Item> Items { get; set; }
        public DbSet<ExplicitMod> ExplicitMod { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
        }
    }
}

我应该怎么做才能得到一张桌子的结果?

我也收到此错误:

  

System.Data.SqlClient.SqlException:'无效的对象名称'ExplicitMod'

1 个答案:

答案 0 :(得分:1)

您的引用需要一些调整。对于一对多,您的ExplicitMod将需要它自己的PK和FK到Item:

if(typeof localStorage === 'undefined') //localStorage won't work server side
        next();

我相信它也将需要EF的无参数构造函数才能动态构建它们。您可能可以使用受保护的或私有的构造函数。

对于项目:

public class ExplicitMod       
{
    public ExplicitMod()
    {}

    public ExplicitMod(string name)
    {
        Name = name;
    }

    [Key]
    public string ExplicitModId { get; set; } 
    [ForeignKey("Item")]
    public string ItemId{ get; set; }
    public string Name { get; set; }
    public virtual Item Item { get; set; }
}

建议初始化集合并使用私有设置器,以节省创建要填充的新实体时的步骤,并使外部代码将集合引用设置为新的,未跟踪的实体集。 (对于单元测试,我通常将设置器标记在内部,以允许单元测试设置存根)

我强烈建议为PK / FK字段而不是字符串使用public class Item { [Key] public string ItemId { get; set; } public virtual ICollection<ExplicitMod> ExplicitMods { get; private set; } = new List<ExplicitMod>(); } int。如果要具有唯一的标识字符串,则将它们添加为附加列/ w唯一约束。使用数字或UUID键是“无意义的键”的一种形式,它节省了索引空间,并使修改值更容易,而不必担心意外更改/使数据关系无效。