Skip to content

Commit

Permalink
Merge pull request #61 from isaacgimenez/feature/exclude-property-att…
Browse files Browse the repository at this point in the history
…ribute

Added option to exclude properties through attribute
  • Loading branch information
rigofunc authored Jul 7, 2021
2 parents b0a57f8 + b0c22e3 commit a613848
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public IActionResult Create()
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("BlogId,Url")] Blog blog)
public async Task<IActionResult> Create([Bind("BlogId,Url,PrivateURL")] Blog blog)
{
if (ModelState.IsValid)
{
Expand Down Expand Up @@ -83,7 +83,7 @@ public async Task<IActionResult> Edit(int? id)
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("BlogId,Url")] Blog blog)
public async Task<IActionResult> Edit(int id, [Bind("BlogId,Url,PrivateURL")] Blog blog)
{
if (id != blog.BlogId)
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace EFGetStarted.AspNetCore.NewDb.Migrations
{
public partial class blog_privateURLProperty : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "PrivateURL",
table: "Blogs",
type: "nvarchar(max)",
nullable: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "PrivateURL",
table: "Blogs");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.0-rtm-35687")
.UseIdentityColumns()
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
.HasAnnotation("ProductVersion", "5.0.0");

modelBuilder.Entity("EFGetStarted.AspNetCore.NewDb.Models.Blog", b =>
{
b.Property<int>("BlogId")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
.HasColumnType("int")
.UseIdentityColumn();

b.Property<string>("Url");
b.Property<string>("PrivateURL")
.HasColumnType("nvarchar(max)");

b.Property<string>("Url")
.HasColumnType("nvarchar(max)");

b.HasKey("BlogId");

Expand All @@ -36,13 +41,17 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
b.Property<int>("PostId")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
.HasColumnType("int")
.UseIdentityColumn();

b.Property<int>("BlogId");
b.Property<int>("BlogId")
.HasColumnType("int");

b.Property<string>("Content");
b.Property<string>("Content")
.HasColumnType("nvarchar(max)");

b.Property<string>("Title");
b.Property<string>("Title")
.HasColumnType("nvarchar(max)");

b.HasKey("PostId");

Expand All @@ -55,21 +64,27 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
.HasColumnType("int")
.UseIdentityColumn();

b.Property<string>("Changed");
b.Property<string>("Changed")
.HasColumnType("nvarchar(max)");

b.Property<DateTime>("Created");
b.Property<DateTime>("Created")
.HasColumnType("datetime2");

b.Property<int>("Kind");
b.Property<int>("Kind")
.HasColumnType("int");

b.Property<string>("RowId")
.IsRequired()
.HasMaxLength(50);
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");

b.Property<string>("TableName")
.IsRequired()
.HasMaxLength(128);
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");

b.HasKey("Id");

Expand All @@ -81,7 +96,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasOne("EFGetStarted.AspNetCore.NewDb.Models.Blog", "Blog")
.WithMany("Posts")
.HasForeignKey("BlogId")
.OnDelete(DeleteBehavior.Cascade);
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

b.Navigation("Blog");
});

modelBuilder.Entity("EFGetStarted.AspNetCore.NewDb.Models.Blog", b =>
{
b.Navigation("Posts");
});
#pragma warning restore 612, 618
}
Expand Down
47 changes: 47 additions & 0 deletions samples/AspNetCore5.0.MVC.EF.Blogs/Models/ContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace EFGetStarted.AspNetCore.NewDb.Models
{
/// <summary>
/// Class need for EF Migrations to know how the Context should be created.
/// This class is not intended for use on the applications.
/// </summary>
public class BloggingContextFactory : IDesignTimeDbContextFactory<BloggingContext>
{
/// <summary>
/// Creates a DbOptionsBuilder from a connectionString.
/// </summary>
/// <param name="connectionString">ConnectionString to apply.</param>
/// <returns>DbContextOptionsBuilder.</returns>
public static DbContextOptionsBuilder CreateDbOptionsBuilder(string connectionString)
{
var options = new DbContextOptionsBuilder();
return SetDbOptions(options, connectionString);
}

/// <summary>
/// Set options to a DbOptionsBuilder.
/// </summary>
/// <param name="options">Options.</param>
/// <param name="connectionString">ConnectionString to apply.</param>
/// <returns>DbContextOptionsBuilder.</returns>
public static DbContextOptionsBuilder SetDbOptions(DbContextOptionsBuilder options, string connectionString)
{
return options.UseSqlServer(connectionString);
}


/// <summary>
/// Create a new DB Context, not intended to be used.
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public BloggingContext CreateDbContext(string[] args)
{
var connection = @"Server=.;Database=AutoHistoryTest;Trusted_Connection=True;ConnectRetryCount=0";
var optionsBuilder = CreateDbOptionsBuilder(connection);
return new BloggingContext(optionsBuilder.Options);
}
}
}
7 changes: 6 additions & 1 deletion samples/AspNetCore5.0.MVC.EF.Blogs/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ namespace EFGetStarted.AspNetCore.NewDb.Models
{
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
public BloggingContext(DbContextOptions options)
: base(options)
{ }

public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<AutoHistory> AutoHistory { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// enable auto history functionality.
Expand All @@ -24,6 +26,9 @@ public class Blog
public int BlogId { get; set; }
public string Url { get; set; }

[ExcludeFromHistory]
public string PrivateURL { get; set; }

public ICollection<Post> Posts { get; set; }
}

Expand Down
5 changes: 5 additions & 0 deletions samples/AspNetCore5.0.MVC.EF.Blogs/Views/Blogs/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<input asp-for="Url" class="form-control" />
<span asp-validation-for="Url" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PrivateURL" class="control-label"></label>
<input asp-for="PrivateURL" class="form-control" />
<span asp-validation-for="PrivateURL" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
Expand Down
15 changes: 12 additions & 3 deletions samples/AspNetCore5.0.MVC.EF.Blogs/Views/Blogs/Delete.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,23 @@
<h4>Blog</h4>
<hr />
<dl class="row">
<dt class = "col-sm-2">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Url)
</dt>
<dd class = "col-sm-10">
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Url)
</dd>
</dl>


<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.PrivateURL)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.PrivateURL)
</dd>
</dl>

<form asp-action="Delete">
<input type="hidden" asp-for="BlogId" />
<input type="submit" value="Delete" class="btn btn-danger" /> |
Expand Down
12 changes: 10 additions & 2 deletions samples/AspNetCore5.0.MVC.EF.Blogs/Views/Blogs/Details.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@
<h4>Blog</h4>
<hr />
<dl class="row">
<dt class = "col-sm-2">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Url)
</dt>
<dd class = "col-sm-10">
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Url)
</dd>
</dl>
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.PrivateURL)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.PrivateURL)
</dd>
</dl>
</div>
<a asp-action="Edit" asp-route-id="@Model.BlogId">Edit</a> |
<a asp-action="Index">Back to List</a>
Expand Down
Loading

0 comments on commit a613848

Please sign in to comment.