本文整理汇总了C#中BloggingContext类的典型用法代码示例。如果您正苦于以下问题:C# BloggingContext类的具体用法?C# BloggingContext怎么用?C# BloggingContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BloggingContext类属于命名空间,在下文中一共展示了BloggingContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EnsureDeleted_will_delete_database_test
private static async Task EnsureDeleted_will_delete_database_test(bool async)
{
using (var testDatabase = await SqlServerTestDatabase.Scratch(createDatabase: true))
{
testDatabase.Connection.Close();
using (var context = new BloggingContext(testDatabase))
{
Assert.True(async ? await context.Database.AsRelational().ExistsAsync() : context.Database.AsRelational().Exists());
if (async)
{
Assert.True(await context.Database.EnsureDeletedAsync());
}
else
{
Assert.True(context.Database.EnsureDeleted());
}
Assert.Equal(ConnectionState.Closed, context.Database.AsRelational().Connection.DbConnection.State);
Assert.False(async ? await context.Database.AsRelational().ExistsAsync() : context.Database.AsRelational().Exists());
Assert.Equal(ConnectionState.Closed, context.Database.AsRelational().Connection.DbConnection.State);
}
}
}
示例2: DefaultModel
protected DefaultModel(string blogusername = null)
{
_IUser = HttpContext.Current.User;
// The user that we're looking at.
string viewUser;
if (_IUser.Identity.IsAuthenticated)
{
viewUser = _IUser.Identity.Name;
}
else if (blogusername != null)
{
viewUser = blogusername;
}
else
{
viewUser = "tgobyrne";
}
using (var db = new BloggingContext())
{
User = (from u in db.Users
where u.Username == viewUser || u.FirstName == viewUser
select u).FirstOrDefault();
Sections = (from p in User.Posts
select p.Section)
.Distinct()
.ToList();
}
HttpContext.Current.Items["User"] = User;
}
示例3: TestGetAllowedEntityTypes
public void TestGetAllowedEntityTypes()
{
var ctx = new BloggingContext("Blogging");
var allowedTypes = DbContextExtensions.DbContextExtensions.GetAllowedEntityTypes<BloggingContext>(ctx);
Assert.IsNotNull(allowedTypes);
Assert.IsTrue(allowedTypes.Count > 0);
}
示例4: AddSiteUpdate
public bool AddSiteUpdate(string headline, string importance)
{
using (var db = new BloggingContext())
{
try
{
var user = (from u in db.Users
where u.Username == this.User.Identity.Name
select u)
.FirstOrDefault();
;
var update = new SiteUpdate()
{
User = user,
Headline = headline,
Importance = importance,
Timestamp = DateTime.Now
};
db.SiteUpdates.Add(update);
db.SaveChanges();
}
catch
{
return false;
}
}
return true;
}
示例5: SaveChanges_logs_DataStoreErrorLogState
public async Task SaveChanges_logs_DataStoreErrorLogState(bool async)
{
var loggerFactory = new TestLoggerFactory();
var serviceProvider = new ServiceCollection()
.AddEntityFramework()
.AddInMemoryStore()
.ServiceCollection()
.AddInstance<ILoggerFactory>(loggerFactory)
.BuildServiceProvider();
using (var context = new BloggingContext(serviceProvider))
{
context.Blogs.Add(new BloggingContext.Blog(jimSaysThrow: false) { Url = "http://sample.com" });
context.SaveChanges();
context.ChangeTracker.Entries().Single().State = EntityState.Added;
Exception ex;
if (async)
{
ex = await Assert.ThrowsAsync<ArgumentException>(() => context.SaveChangesAsync());
}
else
{
ex = Assert.Throws<ArgumentException>(() => context.SaveChanges());
}
Assert.Same(ex, loggerFactory.Logger.LastDataStoreErrorException);
Assert.Same(typeof(BloggingContext), loggerFactory.Logger.LastDataStoreErrorState.ContextType);
Assert.EndsWith(ex.ToString(), loggerFactory.Logger.LastDataStoreErrorFormatter(loggerFactory.Logger.LastDataStoreErrorState, ex));
}
}
示例6: Index
// GET: Sql
public ActionResult Index()
{
using (var context = new BloggingContext())
{
var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList();
}
return View();
}
示例7: SetUp
protected override void SetUp()
{
base.SetUp();
using (var context = new BloggingContext(ConnectionStringEF))
{
context.Blogs.RemoveRange(context.Blogs);
context.Posts.RemoveRange(context.Posts);
context.SaveChanges();
}
}
示例8: Run
public static void Run()
{
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.FromSql("SELECT * FROM dbo.Blogs")
.ToList();
}
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.FromSql("EXECUTE dbo.GetMostPopularBlogs")
.ToList();
}
using (var context = new BloggingContext())
{
var user = "johndoe";
var blogs = context.Blogs
.FromSql("EXECUTE dbo.GetMostPopularBlogsForUser {0}", user)
.ToList();
}
using (var context = new BloggingContext())
{
var user = new SqlParameter("user", "johndoe");
var blogs = context.Blogs
.FromSql("EXECUTE dbo.GetMostPopularBlogsForUser @user", user)
.ToList();
}
using (var context = new BloggingContext())
{
var searchTerm = ".NET";
var blogs = context.Blogs
.FromSql("SELECT * FROM dbo.SearchBlogs {0}", searchTerm)
.Where(b => b.Rating > 3)
.OrderByDescending(b => b.Rating)
.ToList();
}
using (var context = new BloggingContext())
{
var searchTerm = ".NET";
var blogs = context.Blogs
.FromSql("SELECT * FROM dbo.SearchBlogs {0}", searchTerm)
.Include(b => b.Posts)
.ToList();
}
}
示例9: Inserts_and_updates_are_batched_correctly
public void Inserts_and_updates_are_batched_correctly()
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(_testStore.Connection);
var expectedBlogs = new List<Blog>();
using (var context = new BloggingContext(_serviceProvider, optionsBuilder.Options))
{
context.Database.EnsureCreated();
var owner1 = new Owner {Name = "0"};
var owner2 = new Owner {Name = "1" };
context.Owners.Add(owner1);
context.Owners.Add(owner2);
var blog1 = new Blog
{
Id = Guid.NewGuid(),
Owner = owner1,
Order = 1
};
context.Blogs.Add(blog1);
expectedBlogs.Add(blog1);
context.SaveChanges();
owner2.Name = "2";
blog1.Order = 0;
var blog2 = new Blog
{
Id = Guid.NewGuid(),
Owner = owner1,
Order = 1
};
context.Blogs.Add(blog2);
expectedBlogs.Add(blog2);
var blog3 = new Blog
{
Id = Guid.NewGuid(),
Owner = owner2,
Order = 2
};
context.Blogs.Add(blog3);
expectedBlogs.Add(blog3);
context.SaveChanges();
}
AssertDatabaseState(true, expectedBlogs, optionsBuilder);
}
示例10: AdminCenterModel
//public List<Tags> Tags { get; protected set; }
//public List<Blog> Blogs { get; protected set; }
//public List<Album> Albums { get; protected set; }
//public List<Comment> Comments { get; protected set; }
//public List<Picture> Pictures { get; protected set; }
//public List<Post> Posts { get; protected set; }
public AdminCenterModel(BloggingContext context = null)
: base("tgobyrne")
{
if (context != null)
{
Context = context;
}
else
{
Context = new BloggingContext();
}
}
示例11: Exists_returns_true_when_database_exists_test
private static async Task Exists_returns_true_when_database_exists_test(bool async)
{
using (var testDatabase = await SqlServerTestStore.CreateScratchAsync(createDatabase: true))
{
using (var context = new BloggingContext(testDatabase))
{
Assert.True(async ? await context.Database.AsRelational().ExistsAsync() : context.Database.AsRelational().Exists());
Assert.Equal(ConnectionState.Closed, context.Database.AsRelational().Connection.DbConnection.State);
}
}
}
示例12: Exists_returns_false_when_database_doesnt_exist_test
private static async Task Exists_returns_false_when_database_doesnt_exist_test(bool async)
{
using (var testDatabase = await SqlServerTestStore.CreateScratchAsync(createDatabase: false))
{
using (var context = new BloggingContext(testDatabase))
{
var creator = context.GetService<IRelationalDatabaseCreator>();
Assert.False(async ? await creator.ExistsAsync() : creator.Exists());
Assert.Equal(ConnectionState.Closed, context.Database.GetDbConnection().State);
}
}
}
示例13: IsValid
/// <summary>
/// Checks if user with given password exists in the database
/// </summary>
/// <param name="_username">User name</param>
/// <param name="_password">User password</param>
/// <returns>True if user exist and password is correct</returns>
public bool IsValid(string _username, string _password)
{
using (var db = new BloggingContext())
{
var username = _username;
var password = Helpers.SHA1.Encode(_password);
var user = (from u in db.Users
where u.Username == username && u.Password == password
select u).FirstOrDefault();
return user != null;
}
}
示例14: Run
public static void Run()
{
var connectionString = @"Server=(localdb)\mssqllocaldb;Database=EFSaving.Transactions;Trusted_Connection=True;";
using (var context = new BloggingContext(
new DbContextOptionsBuilder<BloggingContext>()
.UseSqlServer(connectionString)
.Options))
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}
var connection = new SqlConnection(connectionString);
connection.Open();
using (var transaction = connection.BeginTransaction())
{
try
{
// Run raw ADO.NET command in the transaction
var command = connection.CreateCommand();
command.Transaction = transaction;
command.CommandText = "DELETE FROM dbo.Blogs";
command.ExecuteNonQuery();
// Run an EF Core command in the transaction
var options = new DbContextOptionsBuilder<BloggingContext>()
.UseSqlServer(connection)
.Options;
using (var context = new BloggingContext(options))
{
context.Database.UseTransaction(transaction);
context.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/dotnet" });
context.SaveChanges();
}
// Commit transaction if all commands succeed, transaction will auto-rollback
// when disposed if either commands fails
transaction.Commit();
}
catch (System.Exception)
{
// TODO: Handle failure
}
}
}
示例15: Inserts_are_batched_correctly
public void Inserts_are_batched_correctly(bool clientPk, bool clientFk, bool clientOrder)
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseSqlServer(_testStore.Connection);
var expectedBlogs = new List<Blog>();
using (var context = new BloggingContext(_serviceProvider, optionsBuilder.Options))
{
context.Database.EnsureCreated();
var owner1 = new Owner();
var owner2 = new Owner();
context.Owners.Add(owner1);
context.Owners.Add(owner2);
for (var i = 1; i < 500; i++)
{
var blog = new Blog();
if (clientPk)
{
blog.Id = Guid.NewGuid();
}
if (clientFk)
{
blog.Owner = i % 2 == 0 ? owner1 : owner2;
}
if (clientOrder)
{
blog.Order = i;
}
context.Blogs.Add(blog);
expectedBlogs.Add(blog);
}
context.SaveChanges();
}
AssertDatabaseState(clientOrder, expectedBlogs, optionsBuilder);
}