0

Question 1.

I have created a database in code-first.

[Column(TypeName="datetime2")]
public DateTime RegistDate { get; set; }

In this way, the table is not created.

modelBuilder.Entity<Test>().Property(f => f.RegistDate).HasColumnType("datetime2");

(In OnModelCreating method) I can be resolved by using Fluent API.

To create a column format datetime2, is there a way only a Fluent API?

Question 2.

I have a domain model as follows.

User Entity Class

public class User
{   
    public Guid UserId { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string UserEmail { get; set; }
    public DateTime JoinDate { get; set; }
    public DateTime LoginDate { get; set; }

    public virtual ICollection<UsersInRole> UsersInRoles { get; set; }
}

Role Entity Class

public class Role
{
    public Guid RoleId { get; set; }
    public string RoleName { get; set; }
    public DateTime CreateDate { get; set; }

    public virtual ICollection<UsersInRole> UsersInRoles { get; set; }
}

UsersInRole Entity Class

public class UsersInRole
{   
    public Guid UserId { get; set; }
    public Guid RoleId { get; set; }
    public DateTime SetDate { get; set; }

    [ForeignKey("UserId")]
    public User User { get; set; }
    [ForeignKey("RoleId")]
    public Role Role { get; set; }
}

This build, the following error will occur

One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'UsersInRole' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'UsersInRoles' is based on type 'UsersInRole' that has no keys defined.

I do not want to add another property Id.

Also, I can not use the [Key] UserId, to RoleId.

There are no other solutions?

tereško
  • 58,060
  • 25
  • 98
  • 150
user2214027
  • 49
  • 1
  • 6
  • For your second question you can create a composite key. You can get more info regarding composite key creation from the link http://stackoverflow.com/questions/5436731/composite-key-as-foreign-key – Saanch Apr 16 '13 at 07:35
  • The second question has been solved see the article link. Thank you~ – user2214027 Apr 16 '13 at 07:45
  • answer to question 1 is here: http://stackoverflow.com/questions/8043816/using-datetime-properties-in-code-first-entity-framework-and-sql-server/8044310#8044310 – qujck Apr 16 '13 at 09:48

0 Answers0