C# 无法在c中的编译时使用模型属性#
我正在Asp.net MVC core中开发一个应用程序,其中我使用一个通用接口,如下所示:C# 无法在c中的编译时使用模型属性#,c#,asp.net,.net,entity-framework,generics,C#,Asp.net,.net,Entity Framework,Generics,我正在Asp.net MVC core中开发一个应用程序,其中我使用一个通用接口,如下所示: public interface IRepository<TEntity, TKeyType> where TEntity : class where TKeyType : struct { TEntity Add(TEntity entity); } public class ShoppingCartRepository<ShoppingCartItem, Int32&g
public interface IRepository<TEntity, TKeyType> where TEntity : class where TKeyType : struct
{
TEntity Add(TEntity entity);
}
public class ShoppingCartRepository<ShoppingCartItem, Int32> : EntityRepository<ShoppingCartItem, Int32>
where ShoppingCartItem : class where Int32 : struct
{
public ShoppingCartRepository(DrinkDbContext context) : base(context) { }
public override ShoppingCartItem Add(ShoppingCartItem entity)
{
base.Add(entity);
return entity;
}
}
公共接口Iposition where tenty:class where TKeyType:struct
{
TEntity Add(TEntity实体);
}
下面是它的实现
public class EntityRepository<TEntity, TKeyType> : IRepository<TEntity, TKeyType> where TEntity : class where TKeyType : struct
{
protected readonly DrinkDbContext Context;
protected readonly DbSet<TEntity> DbSet;
public EntityRepository(DrinkDbContext context)
{
Context = context;
DbSet = context.Set<TEntity>();
}
public virtual TEntity Add(TEntity entity)
{
Context.Add(entity);
return entity;
}
}
public class EntityRepository:IRepository where tenty:class where TKeyType:struct
{
受保护的只读DrinkDbContext上下文;
受保护的只读数据库集;
公共实体报告(DrinkDbContext上下文)
{
上下文=上下文;
DbSet=context.Set();
}
公共虚拟TEntity添加(TEntity实体)
{
添加(实体);
返回实体;
}
}
此外,我在ShoppingCartRepository中继承EntityRepository类,如下所示:
public interface IRepository<TEntity, TKeyType> where TEntity : class where TKeyType : struct
{
TEntity Add(TEntity entity);
}
public class ShoppingCartRepository<ShoppingCartItem, Int32> : EntityRepository<ShoppingCartItem, Int32>
where ShoppingCartItem : class where Int32 : struct
{
public ShoppingCartRepository(DrinkDbContext context) : base(context) { }
public override ShoppingCartItem Add(ShoppingCartItem entity)
{
base.Add(entity);
return entity;
}
}
公共类ShoppingCartRepository:EntityRepository
where ShoppingCartItem:类,其中Int32:struct
{
公共ShoppingCartRepository(DrinkDbContext上下文):基本(上下文){}
公共覆盖ShoppingCartItem添加(ShoppingCartItem实体)
{
基础。添加(实体);
返回实体;
}
}
并且能够在运行时在方法中查看类的填充属性。请查看以下截图:
public interface IRepository<TEntity, TKeyType> where TEntity : class where TKeyType : struct
{
TEntity Add(TEntity entity);
}
public class ShoppingCartRepository<ShoppingCartItem, Int32> : EntityRepository<ShoppingCartItem, Int32>
where ShoppingCartItem : class where Int32 : struct
{
public ShoppingCartRepository(DrinkDbContext context) : base(context) { }
public override ShoppingCartItem Add(ShoppingCartItem entity)
{
base.Add(entity);
return entity;
}
}
但在编译时尝试使用这些属性时,我无法看到这些属性。请找到下面的截图
如何访问在Add方法中作为参数传递的ShoppingCartItem的属性?问题是您的
ShoppingCartRepository
被视为通用存储库。
ShoppingCartRepository
->在此场景中ShoppingCartItem
属于您已对其进行约束的类型class
这就像写购物车存储库
。因此,您的ShoppingCartItem不被视为类型ShoppingCartItem
,而是类型T
的泛型,其中T:class
因此,类
无权访问添加
方法中的ShoppingCartItem
属性
最新情况:
公共类ShoppingCartRepository:EntityRepository
这使得您的购物车存储库非通用。
我认为您不希望使用ShoppingCartRepository
泛型,因为您正在为ShoppingCartItem
指定实现
随着上述变化。您应该可以访问Add
方法中的ShoppingCartItem
属性 虽然你的问题是半完整的,可以理解的。请张贴代码以提高清晰度,并供我们复制。尽管我相信您的问题在于通用约束。@panoskarajohn:请用代码查找更新的问题。公共类ShoppingCartRepository:EntityRepository
将您的代码更改为此。ShoppingCartItem被视为一个类,作为泛型变量。由于您需要特定的功能,因此无需将ShoppingCartRepository设置为通用。感谢更新。我现在可以访问模型属性。在Startup.cs类的前面部分,我定义了如下依赖项:public void ConfigureServices(IServiceCollection services){services.AddTransient(typeof(IRepository),typeof(ShoppingCartRepository));}但现在改为services.AddTransient(typeof(IRepository),typeof(ShoppingCartRepository));我面临依赖注入问题。Pl帮助如何指定ShoppingCartRepository的依赖项。更改DI并没有解决我的问题。你的解决方案帮助了我,这就是为什么我投了赞成票。我已经按照您的建议更新了代码,但现在在Startup.cs中配置服务时遇到了问题。请参见以下内容:public void ConfigureServices(IServiceCollection services){//此行提供编译时错误服务。AddTransient(typeof(IRepository),(ShoppingCartRepository));}@semwal您的DI问题超出范围。但我会继续将其注册为Self()或为其创建一个接口。老实说,你需要更多地了解回购模式。您已经创建了一个通用解决方案,然后继续并继承了它。至少对我来说,这是非常令人困惑的。朱莉·勒曼是EF模式方面的权威。研究她。如果你还有问题的话。随便问一个新问题。@semwal通用解决方案的目的是不必创建特定的解决方案,这意味着您可以像这样做IRepository
,而不必创建特定的实现。你所做的将导致问题。想象一下,添加一个包含其他对象的新存储库,您将如何重新注册它?你需要重新考虑这一点。我认为你做得不对。谢谢。你能在DI上帮我吗。我已经按照您的建议更新了代码,但现在在Startup.cs中配置服务时遇到了问题。请参见以下内容:public void ConfigureServices(IServiceCollection services){//这是在给出编译时错误。services.AddTransient(typeof(IRepository),(ShoppingCartRepository));}现在如何为ShoppingCartRepository定义DI?