使用SQLite EF建模实体

时间:2018-05-01 04:31:24

标签: c# entity-framework sqlite

我有班级老师和SubjectTeacherDetail。我像这样建模:

<h1>Search</h1>
<%= form_tag(search_path, method: :get) do %>
  <%= label_tag :name %>
  <%= text_field_tag :name, params[:name] %>

  <%= label_tag :category_id %>
  <%= select_tag(:category_id, options_for_select(Category.all.collect {|o| [ o.name, o.id ] }, params[:category_id]), { prompt: 'Select Category'}) %>

  <%= submit_tag "Search" %>
<% end %>

<h2><%= pluralize(@projects.count, "Result") %></h2>
<% @projects.each do |project| %>
  <h3><%= project.name %></h3>
<% end %>

我添加这样的实体:

  private static void SubjectTeacherDetailModel(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<SubjectTeacherDetail>().HasKey(s => new { s.Id });
         modelBuilder.Entity<SubjectTeacherDetail>().HasRequired(r => r.Teacher).WithMany(m => m.SubjectTeacherDetail).HasForeignKey(k => k.TeacherId);
    }

当我从DB获取教师时,它返回一个空的SubjectTeacherDetail集合。哪里弄错了?

1 个答案:

答案 0 :(得分:0)

我解决了问题,但有些事情很奇怪。它可以在我从DB获取所有SubjectTeacherDetail时起作用:

 Teachers = new ObservableCollection<Teacher>(DataProvider.GetEntity<Teacher>());
        var detail = DataProvider.GetEntity<SubjectTeacherDetail>().ToList();

任何人都能解释为什么会这样吗?