У меня есть типовая схема таблицы master / detail (таблица User / Settings) (SQL Server) и настройка Entity Framework с использованием Fluent API для работы с этими таблицами.
Я определяю это как независимую связь, поэтомукласс UserProfileSetting
не включает в себя свойство UserId
, но, как я понимаю, оно правильно отображается в конфигурации.
Что ж, моя проблема в том, что когда один элемент Settings
обновляется для профиля,на уровне базы данных настройки обновляются для всех пользователей.В основном USER_ID
не учитывается.
Произведенный SQL-запрос выглядит так:
UPDATE [dbo].[T_USERPROFILE_SETTING]
SET [VALUE] = @0
WHERE ([KEY] = @1)
Есть идеи, что может быть не так?Я предполагаю, что если я наконец добавлю свойство UserId
к UserProfileSettings
, это решит проблему, но я хотел попытаться исправить это без него.
Текущий код ниже ...
Код обновления данных
var entry = profile.Settings.Where(s => s.Key == key).SingleOrDefault();
if (entry != null)
{
entry.Value = value;
} else {
var setting = /* Here create a new setting */
profile.Settings.Add(setting);
}
DataContext.SaveChanges();
Объекты:
public partial class UserProfile
{
[Key]
public string UserId { get; set; }
public DateTimeOffset LastLogin { get; set; }
public ICollection<UserProfileSetting> Settings { get; set; }
}
public class UserProfileSetting
{
public UserProfileSetting() { }
public string Key { get; set; }
public string Value { get; set; }
}
Конфигурация объекта:
public class UserProfileConfiguration : EntityTypeConfiguration<UserProfile>
{
public UserProfileConfiguration()
{
ToTable("T_USERPROFILE");
HasKey<string>(p => p.UserId);
Property(p => p.UserId)
.HasColumnName("USER_ID")
.HasMaxLength(50)
.IsUnicode()
.IsRequired();
Property(p => p.LastLogin)
.HasColumnName("LAST_LOGIN_AT")
.IsRequired();
HasMany<UserProfileSetting>(p => p.Settings)
.WithOptional()
.Map(m => m.MapKey("USER_ID"));
}
}
public class UserProfileSettingConfiguration : EntityTypeConfiguration<UserProfileSetting>
{
public UserProfileSettingConfiguration()
{
ToTable("T_USERPROFILE_SETTING");
HasKey(p => p.Key );
Property(p => p.Key)
.HasColumnName("KEY")
.HasMaxLength(50)
.IsUnicode()
.IsRequired();
Property(p => p.Value)
.HasColumnName("VALUE")
.IsUnicode()
.IsRequired();
}
}