放置列表<>进入SQL数据库表列

时间:2015-12-29 15:05:00

标签: c# database jointable

我在这里遇到了类似的问题,我无法弄清楚解决方案( Retrieve list from object in Database)。

我只想知道是否可以将列表插入SQL数据库以及如何使用查询将其返回。

1 个答案:

答案 0 :(得分:0)

" SQL数据库"是一个具有EF驱动程序的SQL系列数据库?如果是这样,那么您可以先使用代码来保留列表。即:(此样本具有多对多关系)

string defaultConString = @"server=.\SQLExpress;Database=CodeFirstDbSample;Trusted_Connection=yes;";

void Main()
{
    CreateSampleCodeFirstData();
    ListData();
}

private void CreateSampleCodeFirstData()
{
    var ctx = new MyContext(defaultConString);
  for (int i = 0; i < 10; i++)
  {
    var c = new Customer { CustomerName="c" + i };
    ctx.Customers.Add( c );
  }

  for (int i = 0; i < 10; i++)
  {
    var p = new Product { ProductName="p" + i };
    ctx.Products.Add( p );
  }

  ctx.SaveChanges();

  for (int i = 0; i < 10; i++)
  {
    var customer = ctx.Customers.Single (c => c.CustomerName == "c"+i);
    var products = ctx.Products.OrderBy (p => Guid.NewGuid()).Take(3);
    customer.Products = new List<Product>();
    customer.Products.AddRange( products );
  }

  ctx.SaveChanges();
}

private void ListData()
{
  var ctx = new MyContext(defaultConString);
  Console.WriteLine ("By Customer");
  Console.WriteLine ("".PadRight(50,'-'));
  foreach (Customer c in ctx.Customers.Include("Products"))
  {
    Console.WriteLine ("{0}: {1}", c.CustomerId, c.CustomerName);
    Console.WriteLine ("\t\t{0}", 
      string.Join(",", c.Products.Select (p => p.ProductName)));
  }
  Console.WriteLine ("".PadRight(50,'='));
  Console.WriteLine ();

  Console.WriteLine ("By Product");
  Console.WriteLine ("".PadRight(50,'-'));
  foreach (Product p in ctx.Products.Include("Customers"))
  {
    Console.WriteLine ("{0}: {1}", p.ProductId, p.ProductName);
    Console.WriteLine ("\t\t{0}", 
       string.Join(",", p.Customers.Select (c => c.CustomerName) ));
  }
  Console.WriteLine ("".PadRight(50,'='));
  Console.WriteLine ();
}

public class MyContext : DbContext
{
  public MyContext(string connectionString)
     : base(connectionString)  {}
  public DbSet<Customer> Customers { get; set; }
  public DbSet<Product> Products { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public virtual List<Customer> Customers { get; set; }

}

public class Customer
{
    public int CustomerId{ get; set; }
    public string CustomerName{ get; set; }
    public virtual List<Product> Products { get; set; }
}