У сущности нет первичного ключа

Имущество:

[Key]
[DatabaseGeneratedDefaultValue]
public int DateId { get; set; }

Код:

var date = conn.Get(new Date { DateId = dateId });

Ошибка:

Message: System.NotSupportedException : Entity 'Date' has no primary key. SELECT is not possible.

Как я могу настроить первичный ключ, который не является просто "Id"?

Я нашел следующий код в источнике. Если предположить, что он вызывается, мне интересно, почему ему не хватает моего атрибута...

public virtual void ConfigureEntityPropertyMapping(PropertyMapping propertyMapping)
{
    // set the Id property to be the primary database generated key in case we don't find any orm attributes on the entity or on the properties
    if(string.Equals(propertyMapping.PropertyName, "id", StringComparison.OrdinalIgnoreCase)
        && this.GetEntityAttributes(propertyMapping.EntityMapping.EntityType).Length == 0
        && !this.GetEntityProperties(propertyMapping.EntityMapping.EntityType).Any(
            propDesc => this.GetEntityPropertyAttributes(propertyMapping.EntityMapping.EntityType, propDesc).Any(
                propAttr => propAttr is ColumnAttribute || propAttr is KeyAttribute || propAttr is DatabaseGeneratedAttribute)))
    {
        propertyMapping.SetPrimaryKey();
        propertyMapping.ExcludeFromInserts();
        propertyMapping.RefreshOnInserts();
        return;
    }

Я предполагаю, что он игнорирует атрибуты, если предоставляется пользовательское сопоставление.

Сейчас я исправил это с помощью следующей конфигурации...

    OrmConfiguration.SetDefaultEntityMapping(
        OrmConfiguration
            .GetDefaultEntityMapping<Date>()
            .Clone()
            .SetProperty(d => d.DateId, d =>
            {
                d.IsPrimaryKey = true;
                d.DatabaseColumnName = "id";
            });

0 ответов

Другие вопросы по тегам