情報を取得するためにリポジトリを取得する際に問題が発生しています。どんな思考も高く評価されます。C#Entity Frameworkコア&リポジトリ
リポジトリ:
public class CustomerRepository : ICustomerRepository
{
private masterContext context;
public CustomerRepository(masterContext context)
{
this.context = context;
}
public IEnumerable<Customer> GetCustomers()
{
return context.Customer.ToList();
}
public Customer GetCustomerById(int customerId)
{
var result = (from c in context.Customer where c.CustomerId == customerId select c).FirstOrDefault();
return result;
}
public void Save()
{
context.SaveChanges();
}
コントローラー:
public partial class masterContext : DbContext
{
public masterContext(DbContextOptions<masterContext> options)
: base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(entity =>
{
entity.Property(e => e.CustomerName).IsRequired();
});
}
public virtual DbSet<Customer> Customer { get; set; }
public virtual DbSet<Order> Order { get; set; }
}
これはおそらくあなたの問題を解決するものではありませんが、なぜienumerableを返すときにリストを使用するのですか?それはリソースの無駄です;) –
そして私はあなたが方法を使用するときにキャストよりもあなたが実現しました。それはトリプルキャストです –
ああ、そうです。あなたはCustomerRepositoryのインスタンスを作成する必要はありません。あなたはDepenency injectionを使用しますか? –