Reputation: 73
I have a model class:
public Account()
{
}
public Account(Guid id, int accountnumber, string accountType)
{
Id = id;
Accountnumber = accountnumber;
AccountType = accountType;
}
public Guid Id { get; private set; }
public int Accountnumber { get; private set; }
public string AccountType { get; private set; }
}
And a DbContext class:
class BankContext : DbContext
{
public DbSet<Account> Accounts { get; set; }
}
and the main class where I am trying add the values.
Account account = new Account();
BankContext bank = new BankContext();
bank.Accounts.Add(new Account(Guid.NewGuid(), 343, "Checkings")
{
Id=Guid.NewGuid();// returns error."Id is not accessible"
});
I did good research before I decided to ask help from stackoverflow. I would really appreciate if somebody could guide me on how to add values to the private elements in this scenario.
Upvotes: 0
Views: 445
Reputation: 5284
In your Class public Guid Id { get; private set; }
.Where Id is private set.
so you can't set the Id value publicly. Means you can't
new Account(...)
{
Id=Guid.NewGuid();
}
It always return "Id is not accessible" error.
Another think why you need to set it. You already set it by using constructor.
bank.Accounts.Add(new Account(Guid.NewGuid(), 343, "Checkings"));
It's jest fine.
Upvotes: 1