【问题标题】:Unable to use model properties during compile time in c#在 C# 的编译期间无法使用模型属性
【发布时间】:2020-02-01 09:41:58
【问题描述】:

我正在使用 Asp.net MVC 核心开发一个应用程序,其中我使用如下通用接口:

public interface IRepository<TEntity, TKeyType> where TEntity : class where TKeyType : struct
{
    TEntity Add(TEntity entity);
}

下面是它的实现。

  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;
    }
}

此外,我正在继承 ShoppingCartRepository 中的 EntityRepository 类,如下所示:

 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 的属性?

【问题讨论】:

  • 虽然你的问题是半完整且可以理解的。请张贴代码以获得更清晰的信息,以便我们复制它stackoverflow.com/help/how-to-ask。我会这么说,尽管我相信您的问题在于通用约束。
  • @panoskarajohn : 请使用代码找到更新后的问题。
  • public class ShoppingCartRepository : EntityRepository&lt;ShoppingCartItem, Int32&gt; 将您的代码更改为此。 ShoppingCartItem 被视为一个类作为一个通用变量。由于您需要特定功能,因此无需将 ShoppingCartRepository 设为通用。

标签: c# asp.net .net entity-framework generics


【解决方案1】:

问题是您的ShoppingCartRepository 被视为通用名称。 ShoppingCartRepository&lt;ShoppingCartItem,int&gt; -> 在这种情况下,ShoppingCartItem 的类型为 class,您已对其进行了约束。

这就像写ShoppingCartRepository&lt;T,int&gt;。因此,您的 ShoppingCartItem 不被视为ShoppingCartItem 类型,而是T 类型的泛型@ 987654328@。

所以classAdd 方法中无法访问ShoppingCartItem 的属性。

对此进行更新:

public class ShoppingCartRepository : EntityRepository&lt;ShoppingCartItem, Int32&gt;

这使您的ShoppingCartRepository 非通用。 我认为您不需要 ShoppingCartRepository 泛型,因为您正在为 ShoppingCartItem 指定实现。

有了上面的改动。您应该可以访问 Add 方法中的 ShoppingCartItem 属性。

【讨论】:

  • 感谢更新。我现在可以访问模型属性。在 Startup.cs 类的早期,我已经定义了这样的依赖项: public void ConfigureServices(IServiceCollection services) { services.AddTransient(typeof(IRepository), typeof(ShoppingCartRepository));但现在更改为 services.AddTransient(typeof(IRepository), typeof(ShoppingCartRepository));我正面临依赖注入问题。请帮助如何指定 ShoppingCartRepository 的依赖项。
  • 更改 DI 并没有解决我的问题。您的解决方案帮助了我,这就是我投票支持它的原因。我已经按照您的建议更新了我的代码,但现在在 Startup.cs 中配置服务时我遇到了问题。请看如下: public void ConfigureServices(IServiceCollection services) { // 这行给出编译时错误 services.AddTransient(typeof(IRepository), (ShoppingCartRepository)); }
  • @semwal 您的 DI 问题超出了范围。但我会继续将它注册为 Self() 或为它创建一个接口。老实说,您需要阅读有关回购模式的更多信息。您已经创建了一个通用解决方案,然后您继续并从中继承。这至少对我来说非常令人困惑。 Julie Lerman 是 EF 模式的权威。研究她。另外,如果您仍然有问题。欢迎提出新问题。
  • @semwal 通用解决方案的目的是不必创建特定的解决方案,这意味着您可以像IRepository&lt;ShippingItem,int&gt; 那样做某事,而不是创建特定的实现。你所做的会导致问题。想象一下添加一个带有一些其他对象的新存储库,您将如何注册它?你需要重新考虑这一点。在我看来你做错了。
  • 谢谢。你能帮我解决 DI 问题吗?我已经按照您的建议更新了我的代码,但现在在 Startup.cs 中配置服务时我遇到了问题。请看如下: public void ConfigureServices(IServiceCollection services) { // 这是编译时错误。 services.AddTransient(typeof(IRepository), (ShoppingCartRepository));现在如何为 ShoppingCartRepository 定义 DI?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多