2016-04-07 6 views
1

私はSqlデータベースに2つのテーブルを持っています。たとえば、UserとUserEducationテーブル。 ユーザテーブルには複数のレコードがあり、ユーザごとにUserEducation Tableに1つのレコードがあります。私はグリッドビューでこれらの2つのテーブルを結合してすべてのレコードを表示したいですか? Entity Frameworkを使用してこれを行うにはどうすればよいですか?最初のテーブルからのレコードのリストの2番目のテーブルベースから複数のレコードを取得する方法EFを使用して

答えて

0

またはエンティティへのLINQを使用してテーブルを結合:

using (var context = new YourContext) 
    { 
    var users = context.UserDbSet; 
    var userEdications = context.UserEdication.DbSet ; 

    var joinedTables = from user in users 
         join userEdication in userEdications on user.userId = userEdication.userId 
         select new OutPutEntity 
         { 
           Name = user.Name, 
           Edication = userEdication.Edication 
         } 

         gridView.DataSource = joinedTables.toList(); // should be placed outside the using. (here just as a sample) 
    } 

利点 - あなたはIQurableレベルで出力形式を指定することができます。欠点 - それは複雑に見えます。

1

あなたは、単にユーザーからnavigationpropertyを使用することができます:1つのマッピング一つにあり

var user = new User(); 
user.UserEducation.[Property]; 
+1

ユーザテーブルのリストを取得する方法ユーザテーブルのリストに基づく – sa227

1

以来、私は1つのテーブルを作る好むだろう。しかし具体的には次のようにすることができます:

entityframeworkcontext obj = new entityframeworkContext(); xyz = obj.database.SqlQuery( "modeltablefield1、u.fieldname2をmodeltablefield2、ued.fieldname1をmodeltablefield3、ued.fieldname2をmodeltablefield4として選択してください。ユーザーが内部メンバーになるには、UserEducationをu.commonfield = ued.commonfieldに追加してください。 ");

ここでは、共通のフィールドは2番目のテーブルの外部キーになります モデルテーブルは、複合クエリ(MVC固有)から必要な論理テーブルです。

1

私はあなたのユースケースに合わせてコンソールアプリケーションを作成しました。出力をグリッドビューにバインドするWindowsフォームまたはWebベースのアプリケーションにこのコードを再利用する必要があります。私はPOCOクラスのいくつかのプロパティを想定しています。ユーザーや教育エンティティに保存するすべての値に基づいて、いつでも変更できます。私は自分のコードスニペットに接続文字列について言及していません。 Entityフレームワークは、接続文字列が指定されていない場合、またはapp.configファイルまたはweb.configファイルに接続文字列が記述されているデータベースに接続する場合、コンピュータ上のSQL Expressデータベースに自動的に接続します。お役に立てれば!

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 

namespace UsersCodeFirst 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      using (var db = new EfContext()) 
      { 
       // Display all users with their education from the database 
       var query = from user in db.Users 
          join userEducation in db.UserEducations 
          on user.UserId equals userEducation.UserId 
        orderby user.Name 
        select new 
        { 
         Name = user.Name, 
         UserEducation = userEducation.CourseName 
        }; 

       //bind to grid by setting grid data source to query.ToList() 
      } 

      Console.WriteLine("Press any key to exit..."); 
      Console.ReadKey(); 
     } 
    } 

    public class User 
    { 
     public string UserId { get; set; } 
     public string Name { get; set; } 
     public virtual List<Education> Posts { get; set; } 
    } 

    public class Education 
    { 
     public string EducationId { get; set; } 
     public string CourseName { get; set; } 
     public string UserId { get; set; } 
    } 

    public class EfContext : DbContext 
    { 
     public DbSet<Education> UserEducations { get; set; } 
     public DbSet<User> Users { get; set; } 
    } 
} 

DbContextクラスへの参照を取得するには、プロジェクトにEntity Framework Nugetパッケージを追加する必要があります。

+0

良い説明、私はウラジミール – sa227

関連する問題