简体   繁体   English

如何在 C#/实体框架中的 NotMapped 属性的 setter 中使用映射属性

[英]How can I use a mapped property in the setter of a NotMapped property in C# / Entity Framework

I want to use the value of a property called DirFotos that is mapped to column in the database table to retrieve the photos of a product.我想使用映射到数据库表中的列的名为DirFotos的属性的值来检索产品的照片。 The photos of the product will be a list of string.产品的照片将是一个字符串列表。 That list is not mapped because I will retrieve it from the file system.该列表未映射,因为我将从文件系统中检索它。 Is it possible to use a mapped property in the setter of a NotMapped property in C# / Entity Framework?是否可以在 C#/实体框架中的NotMapped属性的 setter 中使用映射属性? Besides photos is returning an empty list when I perform a query using the Entity Framework.当我使用实体框架执行查询时,除了照片返回一个空列表。 Why is the NotMapped为什么是NotMapped

class Product

    private List<string> _photos = new List<string>();

    [Column("dir_fotos")]
    public String DirFotos { get; set; }   

    [NotMapped]
    public List<String> photos
    {
        get { return _fotos; }
        set
        {            
            // GetPhotos(this.dirFotos) dirFotos is null here
            _photos.Add("teste1.jpg");
            _photos.Add("teste2.jpg");
        }
    }

So in summary I have two problems, I cannot use the this.dirFotos inside the setter of the photos property (it's null) and with this static snippet photos is returning an empty list when I retrieve the products using Entity Framework.所以总而言之,我有两个问题,我不能在照片属性的 setter 中使用this.dirFotos (它是空的),并且当我使用实体框架检索产品时,这个静态片段照片返回一个空列表。

From what I make out from the question:从我从问题中得出的结论:

  • You have an entity to a table which contains a Directory Path for where to find the photographs.您有一个表的实体,其中包含用于查找照片的目录路径。
  • You want to have a list of photographs which will be loaded based on the files in that directory.您希望有一个将根据该目录中的文件加载的照片列表。
  • You're finding that the Directory can be #null when you try to load these photographs in the non-mapped property.当您尝试在非映射属性中加载这些照片时,您会发现 Directory 可以为 #null。

I suspect that the basic premise you have written should work, however a couple of important details.我怀疑您编写的基本前提应该可以工作,但是有一些重要的细节。 For the non-mapped Photos list, you do not need, nor should use a setter.对于未映射的照片列表,您不需要也不应该使用 setter。 Instead it just needs a getter:相反,它只需要一个吸气剂:

private List<string> _photos = null;
private string _dirPhotos = null;

[Column("dir_fotos")]
public String DirPhotos 
{ 
    get { return _dirPhotos; }
    set
    { 
        if(string.Compare(_dirPhotos, value, true) != 0)
        {
            _dirPhotos = value;   
            _photos = null;
        }
    }
}

[NotMapped]
public List<String> photos
{
    get { return _photos ?? (_photos = GetPhotos(DirPhotos) ); }
}

What's important here is that your GetPhotos starts with this assertion as well:这里重要的是您的 GetPhotos 也以这个断言开头:

private List<string> GetPhotos(string dirPhotos)
{
    if (string.IsNullOrEmpty(dirPhotos))
        return new List<string>();

    // ... load photos...
}

The key differences here:这里的主要区别:

  • The DirPhotos is given a variable and getter/setter. DirPhotos 被赋予一个变量和 getter/setter。 If the Directory changes we want to "reset" the List variable so that it can reload when next accessed.如果目录更改,我们希望“重置”列表变量,以便下次访问时可以重新加载。

  • The list initializes to #null as the default value.该列表初始化为#null 作为默认值。 When the "getter" is called, this will check if the variable is null, and if so, call your GetPhotos method.当调用“getter”时,这将检查变量是否为空,如果是,则调用您的 GetPhotos 方法。 Once that variable is set it will reuse that loaded data until the directory is changed.设置该变量后,它将重用加载的数据,直到更改目录。

  • The GetPhotos will return an empty list if the Directory passed in is null/empty.如果传入的 Directory 为空/空,则 GetPhotos 将返回一个空列表。 You will also want to ensure that your GetPhotos method asserts that any directory passed in is valid and either bubble up an exception or return an empty list.您还需要确保您的 GetPhotos 方法断言传入的任何目录都是有效的,并且要么冒泡出异常要么返回一个空列表。 Don't return #null though as we don't want to encounter null ref exceptions down the road.不要返回#null,因为我们不想在路上遇到空引用异常。

I believe the issue you were encountering was that by trying to use a Setter you could be attempting to "set" the collection before the entity has had a chance to load it's data.我相信您遇到的问题是,通过尝试使用 Setter,您可能会尝试在实体有机会加载其数据之前“设置”集合。 With this pattern where the load occurs on the getter you just load the entity like normal and access the DirPhotos after the entity has been read from the DbSet.使用这种加载发生在 getter 上的模式,您只需像平常一样加载实体,并在从 DbSet 读取实体后访问 DirPhotos。 The first access of the DirPhotos will attempt to load the photos from the file system, but just be sure to handle that the entity row may not have a directory set. DirPhotos 的第一次访问将尝试从文件系统加载照片,但请务必处理实体行可能没有设置目录。 (or a valid directory) (或有效目录)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用NotMapped属性并在实体框架中的Json Post中 - Use NotMapped property and in Json Post in Entity Framework 尝试序列化[NotMapped] Entity Framework属性以供Breeze使用 - Trying to serialize [NotMapped] Entity Framework property for use by Breeze 实体框架如何填充需要联接的NotMapped属性? - Entity Framework How to fill NotMapped Property that need a join? 在实体框架中,如何让属性设置器将属性标记为在快照模式下已更改 - In Entity Framework, how can I have a property setter mark the property as changed in snapshot mode C# Entity Framework 6 Code First:如何为存储在 DB 中的属性定义特定的 setter - C# Entity Framework 6 Code First: how to define a specific setter for a property stored in DB 如何从属性设置器访问实体框架实体的上下文? - How can I get to the context of an Entity Framework entity from a property setter? C# 实体框架 NotMapped 单位转换器 - C# Entity Framework NotMapped Unit Converter 实体框架-检查属性是否已映射 - Entity Framework - Check if property is mapped 实体框架,查询中未映射的属性 - Entity Framework, not mapped property in query 没有设置器的C#属性-如何从构造函数获取设置? - C# Property with no setter - how can it get set from constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM