【发布时间】:2013-01-05 11:39:54
【问题描述】:
我有以下代码:
using System;
using System.Collections.Generic;
using System.Text;
using ProjectBase.Utils;
namespace EnterpriseSample.Core.Domain
{
public class Notifikasi : DomainObject<string>, IHasAssignedId<string>
{
public string Name { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public DateTime CreatedDate { get; set; }
public string CreatedBy { get; set; }
public DateTime UpdateDate { get; set; }
public string UpdateBy { get; set; }
public void SetAssignedIdTo(string assignedId)
{
Check.Require(!string.IsNullOrEmpty(assignedId), "assignedId may not be null or empty");
// As an alternative to Check.Require, which throws an exception, the
// Validation Application Block could be used to validate the following
Check.Require(assignedId.Trim().Length == 5, "assignedId must be exactly 5 characters");
ID = assignedId.Trim().ToUpper();
}
public override int GetHashCode()
{
return (GetType().FullName + "|" +
ID + "|" +
Name).GetHashCode();
}
}
}
我在不同的类中使用 Notifikasi 类,但oNoti.ID = "12"; 产生错误:
属性或索引器不能在此上下文中使用,因为集合 访问器无法访问。
谁能帮我解决这个问题? (如果相关,我会使用 ORM)。
INotifikasiDao dao = DaoFactory.GetNotifikasiDao();
dao.closeSession();
dao.openSession();
EnterpriseSample.Core.Domain.Notifikasi oNoti = new EnterpriseSample.Core.Domain.Notifikasi();
int no = 1;
oNoti.Name = "ETL Account History";
DateTime startDate = DateTime.Today;
DateTime expiryDate = startDate.AddDays(30);
oNoti.StartDate = startDate;
oNoti.EndDate = expiryDate;
oNoti.Description = "ACC_HIST" + startDate + "Failure";
oNoti.ID = "12"; //this is where the error happens
dao.Update(oNoti);
【问题讨论】:
-
请出示
ID的声明好吗? -
@O.R.Mapper 我的 ID 是 Identity,所以我不需要声明查看我的代码 ID = assignedId.Trim().ToUpper();
-
ID是您代码中的标识符,因此它当然需要/在某处声明。只有看到声明,我们才能明白为什么编译器不想让你写入ID属性。 -
也许ID声明这样
public int ID{ get; private set; } -
我也碰到过这种问题。我有“加入”,我最终选择了一个新实体。我也需要这个“受保护的集”字段......不知道如何克服这个问题。
标签: c#