http://msdn.microsoft.com/en-us/data/jj591583(v=msdn.10).aspx
- public class Blog
- {
- public int Id { get; set; }
- public string Title { get; set; }
- public string BloggerName { get; set;}
- public virtual ICollection<Post> Posts { get; set; }
- }
-
- public class Post
- {
- public int Id { get; set; }
- public string Title { get; set; }
- public DateTime DateCreated { get; set; }
- public string Content { get; set; }
- public int BlogId { get; set; }
- public ICollection<Comment> Comments { get; set; }
- }
- public class Blog
- {
- [Key]
- public int PrimaryTrackingKey { get; set; }
- public string Title { get; set; }
- public string BloggerName { get; set;}
- public virtual ICollection<Post> Posts { get; set; }
- }
- [Required]
- public string Title { get; set; }
- [MaxLength(10),MinLength(5)]
- public string BloggerName { get; set; }
- [MaxLength(10, ErrorMessage="BloggerName must be 10 characters or less"),MinLength(5)]
- public string BloggerName { get; set; }
- [NotMapped]
- public string BlogCode
- {
- get
- {
- return Title.Substring(0, 1) + ":" + BloggerName.Substring(0, 1);
- }
- }
- public class BlogDetails
- {
- public DateTime? DateCreated { get; set; }
-
- [MaxLength(250)]
- public string Description { get; set; }
- }
- [ComplexType]
- public class BlogDetails
- {
- public DateTime? DateCreated { get; set; }
-
- [MaxLength(250)]
- public string Description { get; set; }
- }
- [ConcurrencyCheck, MaxLength(10, ErrorMessage="BloggerName must be 10 characters or less"),MinLength(5)]
- public string BloggerName { get; set; }
- where (([PrimaryTrackingKey] = @4) and ([BloggerName] = @5))
- @4=1,@5=N'Julie'
- [Timestamp]
- public Byte[] TimeStamp { get; set; }
- [Table("InternalBlogs")]
- public class Blog
- [Column(“BlogDescription", TypeName="ntext")]
- public String Description {get;set;}
- [DatabaseGenerated(DatabaseGenerationOption.Computed)]
- public DateTime DateCreated { get; set; }
- public class Post
- {
- public int Id { get; set; }
- public string Title { get; set; }
- public DateTime DateCreated { get; set; }
- public string Content { get; set; }
- public int BlogId { get; set; }
- [ForeignKey("BlogId")]
- public Blog Blog { get; set; }
- public ICollection<Comment> Comments { get; set; }
- }
- public Person CreatedBy { get; set; }
- public Person UpdatedBy { get; set; }
- public class Person
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public List<Post> PostsWritten { get; set; }
- public List<Post> PostsUpdated { get; set; }
- }
- [InverseProperty("CreatedBy")]
- public List<Post> PostsWritten { get; set; }
-
- [InverseProperty("UpdatedBy")]
- public List<Post> PostsUpdated { get; set; }
Examining the Edit Methods and Edit View
By Rick Anderson|
In this section, you'll examine the generated
Edit
action methods and views for the movie controller. But first will take a short diversion to make release date look better. Open the Models\Movie.cs file and add the highlighted lines shown below:using System; using System.ComponentModel.DataAnnotations; using System.Data.Entity; namespace MvcMovie.Models { public class Movie { public int ID { get; set; } public string Title { get; set; } [Display(Name="Release Date")] [DataType(DataType.Date)] public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } } }
We'll cover DataAnnotations in the next tutorial. The Display attribute specifies what to display for the name of a field (in this case "Release Date" instead of "ReleaseDate"). The DataType attribute specifies the type of the data, in this case it's a date, so the time information stored in the field is not displayed.
Note: A bug in the Chrome browser renders Date formats incorrectly in the Edit view due to incomplete HTML5 support. To work around this bug use the following code:
public class Movie { public int ID { get; set; } public string Title { get; set; } [Display(Name = "Release Date")] // [DataType(DataType.Date)] [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)] public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } }
Run the application and browse to the
Movies
controller by appending /Movies to the URL in the address bar of your browser. Hold the mouse pointer over an Edit link to see the URL that it links to.
Комментариев нет:
Отправить комментарий