Migration Failed ASP.Net API - asp.net

I have problem when I'm trying to update the database.
I'm making web-api with asp.net.
I just added new migration and works perfectly. Then updated my database with update-database command. Then I change some codes. When I add new migration it said:
Unable to generate an explicit migration because the following explicit migrations are pending:
[201705310248355_changeProduct, 201706191007322_ModifyModel, 201707170631209_changeProductModel, 201707170652556_EditProductModelLagi, 201707180312064_AddStorageIdOnInventory, 201707190243206_ChangeRepository, 201708091124374_EditInventory, 201803130702299_testBoom].
Apply the pending explicit migrations before attempting to generate a new explicit migration.
but last time I updated the database it works perfectly
PM> update-database -target testboom -startupprojectname ShopDiaryProject.EF -force
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is already at version 201803130702299_testBoom.
Soo, I tried to add-migration with -force parameter.
It works!
BUT,
I update the database again, the migration content likes start from beginning and can't add tables.
Here are my migration:
public override void Up()
{
CreateTable(
"dbo.Categories",
c => new
{
Id = c.Guid(nullable: false),
Name = c.String(maxLength: 200),
Description = c.String(maxLength: 200),
UserId = c.Guid(nullable: false),
CreatedDate = c.DateTime(nullable: false),
ModifiedDate = c.DateTime(),
DeletedDate = c.DateTime(),
CreatedUserId = c.String(),
ModifiedUserId = c.String(),
DeletedUserID = c.String(),
IsDeleted = c.Boolean(nullable: false),
User_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.User_Id)
.Index(t => t.User_Id);
CreateTable(
"dbo.Products",
c => new
{
Id = c.Guid(nullable: false),
Name = c.String(maxLength: 250),
BarcodeId = c.String(maxLength: 250),
CategoryId = c.Guid(nullable: false),
CreatedDate = c.DateTime(nullable: false),
ModifiedDate = c.DateTime(),
DeletedDate = c.DateTime(),
CreatedUserId = c.String(),
ModifiedUserId = c.String(),
DeletedUserID = c.String(),
IsDeleted = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Categories", t => t.CategoryId, cascadeDelete: true)
.Index(t => t.CategoryId);
CreateTable(
"dbo.Shopitems",
c => new
{
Id = c.Guid(nullable: false),
Quantity = c.Int(nullable: false),
price = c.Decimal(nullable: false, precision: 18, scale: 2),
ProductId = c.Guid(nullable: false),
ShoplistId = c.Guid(nullable: false),
CreatedDate = c.DateTime(nullable: false),
ModifiedDate = c.DateTime(),
DeletedDate = c.DateTime(),
CreatedUserId = c.String(),
ModifiedUserId = c.String(),
DeletedUserID = c.String(),
IsDeleted = c.Boolean(nullable: false),
})
and it said There is already an object named 'Categories' in the database.
Any ideas?

Related

Mocking of Where method in Data Repository is returning null at its actual implementation in repository

I have written a test for edit model of a class in EF core.
public async Task<Expense> EditExpense(Expense model)
{
var expense = _dataRepository.Where<Expense>(x => x.Id == model.Id).ToList();
if(expense != null)
{
expense.ForEach(x =>
{
x.Name = model.Name;
x.AddedOn = model.AddedOn;
x.Amount = model.Amount;
x.Type = model.Type;
x.SplitOption = model.SplitOption;
x.Notes = model.Notes;
x.IsGroupExpense = model.IsGroupExpense;
});
return expense.FirstOrDefault();
}
else
{
return null;
}
}
I want to test this method using xUnit and Moq and I have also written a method for it as below.
[Fact]
public async void UnitTest_IsExpenseEdited_ReturnsTrue()
{
var expenseCurrent = new Expense()
{
Id = 5,
Name = "Test Expense",
Amount = 1500,
AddedBy = "44db32c3-ad6a-4d68-a683-862be363f59c",
AddedOn = DateTime.Now,
Notes = "",
IsDeleted = false,
IsGroupExpense = false,
SplitOption = 1,
Type = 0
};
var expenseList = (new List<Expense>{ expenseCurrent });
var expenseAfterEdit = new Expense()
{
Id = 5,
Name = "Test Expense Edited",
Amount = 2000,
AddedBy = "44db32c3-ad6a-4d68-a683-862be363f59c",
AddedOn = DateTime.Now,
Notes = "Edited !!!",
IsDeleted = false,
IsGroupExpense = true,
SplitOption = 2,
Type = 0
};
_dataRepositoryMock.Setup(x => x.Where<Expense>(a => a.Id == expenseCurrent.Id)).Returns(expenseList as IQueryable<Expense>);
var expenseEdited = await _exepenseRepository.EditExpense(expenseAfterEdit);
Assert.Equal(expenseEdited, expenseAfterEdit);
}
But here, the Where method is returning null
public async Task<Expense> EditExpense(Expense model)
{
var expense = _dataRepository.Where<Expense>(x => x.Id == model.Id).ToList(); in repository
}
I am getting var expense null in above code.
Please suggest what should I include in the code to get the value in above var expense?
To test this method, I want to create a fake data which is going to be updated. Please suggest how this thing needs to be written properly?

Invalid column name in Entity Framework

I have an ASP.MET application. I performed the tutorial on code first migrations. When I executed the commands, the following code was generated for the user table:
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
ApplicationId = c.Guid(nullable: false),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: true),
UserName = c.String(nullable: false, maxLength: 256),
Discriminator = c.String(),
LegacyPasswordHash = c.String(),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
My problem is that I don't need some columns. So, I removed the code from those columns and updated my DB. But, when I try to authenticate a user, I get this error:
Invalid column name 'EmailConfirmed'.
Invalid column name 'PhoneNumberConfirmed'.
Invalid column name 'TwoFactorEnabled'.
Invalid column name 'LockoutEndDateUtc'.
Invalid column name 'LockoutEnabled'.
Invalid column name 'AccessFailedCount'.
But these columns no longer exist because I deleted them.
Or, is it possible to put it back and assign them default values ​​for example:
EmailConfirmed = false
PhoneNumberConfirmed = false
to avoid making an insertion when creating a user. I use EF 6.2.0
Need help please.

Foreign key between tbls

i create Table with Code First and create table with fluentapi .
how can i create Foreign key between them ?
public partial class Tbl_Gender
{
[Key]
public int GendID { get; set; }
[Required]
[StringLength(150)]
public GenType GendType { get; set; }
}
public enum GenType
{
Male = 1,
Fmale = 2
}
}
.
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(),
Family = c.String(),
UserPhoto = c.String(),
RoleID = c.String(),
Gender = c.String(),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
/*******************************************************************************/

Create a inventory movement journal in Dynamics AX 2012

This problem is driving me nuts... I don't know how to fix it. I'm trying to create an inventory movement journal entry using the ProfitLossJournalService.
The code below runs, but it shows the transaction in the "inventory adjustment" journal. I want it in the inventory movement jounal.
public void UpdateInventoryForConsumableItem(InventoryUpdate update)
{
var svc = new ProfitLossJournalServiceClient();
var cc = new CallContext {Company = "SS"};
var journal = new AxdProfitLossJournal();
var table = new AxdEntity_InventJournalTable
{
InventDimFixedSpecified = false,
InventSiteId = "MC",
InventLocationId = "MAIN WH",
JournalType = AxdEnum_InventJournalType.Movement,
JournalNameId = "InvMovJour",
Description = Constants.InventoryMovementJournalDescription,
NumOfLines = 1,
NumOfLinesSpecified = true,
JournalTypeSpecified = true,
JournalId = "InvMovJour",
JournalIdOrignal = "InvMovJour",
action = AxdEnum_AxdEntityAction.update
};
var inventoryDims = new AxdEntity_InventDim
{
InventLocationId = "MAIN WH",
InventDimId = update.InventoryDimId,
InventColorId = update.Color,
InventSiteId = "MC",
InventSizeId = update.Size,
InventStyleId = update.Style,
action = AxdEnum_AxdEntityAction.create
};
var entry = new AxdEntity_InventJournalTrans
{
InventDimId = update.InventoryDimId,
TransDate = DateTime.Now,
ItemId = update.ItemId,
CostMarkupSpecified = false,
InventRefTypeSpecified = false,
LineNumSpecified = false,
JournalType = AxdEnum_InventJournalType.Movement,
JournalTypeSpecified = true,
Qty = update.Quantity,
QtySpecified = true,
InventDim = new AxdEntity_InventDim[1] {inventoryDims}
};
table.InventJournalTrans = new[] {entry};
journal.InventJournalTable = new[] {table};
try
{
var result = svc.create(cc, journal);
Logger.LogTrace(string.Format("sending InventoryTransferJournal {0}", result.GetValue(0)));
Logger.LogEvent(Logger.SentConsumableInventoryUpdateToDynamics);
}
catch (Exception e)
{
Logger.LogException(e);
throw;
}
}
When you use the ProfitLossJournalService that is essentially what you are creating, an Inventory adjustment.
If you want to create an inventory movement journal, unfortunately there is no standard AIF service that does that. You would need to implement that yourself and expose it to AIF.

Multiple setups for a mockset ASP.net only carries out 1 mock

So my problem is that I've got to mock two things. One is a method that finds a person and the other is a list of those people over which it iterates. However it doesn't get both setups but only 1. I've tried putting them in different orders and each time only the top 1 works.
here's my code:
Person test = new Person()
{
City = "Eindhoven Area, Netherlands.",
userid = 1,
ID = 1,
Email = "fraylight#gmail.com",
ExtraInfo = "blabla",
HobbyProjectICTRelated = "a",
Hobbys = "",
LearntSkillsAndLevelOfSkills = "Java:7, C#:4, Software Documentation:4, Software Development:4, HTML:2, CSS:2, jQuery:1",
Name = "Marijn van Donkelaar",
PhoneNr = "0612345678",
ProfileImage = "/Images/hollemar.jpg",
SkillsToLearn = "ASP.net:2, JAVA:3",
Stand = "",
Summary = "",
YearsOfWorkExperience = 6,
PeopleManagerApproved = true,
PeopleManager = "Richard"
};
Person test1 = new Person()
{
City = "Eindhoven Area, Netherlands.",
userid = 2,
ID = 2,
Email = "fraylight#gmail.com",
ExtraInfo = "",
HobbyProjectICTRelated = "a",
Hobbys = "zwemmen",
LearntSkillsAndLevelOfSkills = "Java:8, C#:4, Software Documentation:4, Software Development:4, HTML:2, CSS:2, jQuery:1",
Name = "Richard Holleman",
PhoneNr = "",
ProfileImage = "/Images/hollemar.jpg",
SkillsToLearn = "ASP.net:2, JAVA:2",
Stand = "",
Summary = "",
YearsOfWorkExperience = 16,
PeopleManagerApproved = true,
PeopleManager = "Richard"
};
Person test2 = new Person()
{
City = "Eindhoven Area, Netherlands.",
userid = 3,
ID = 3,
Email = "fraylight#gmail.com",
ExtraInfo = "",
HobbyProjectICTRelated = "",
Hobbys = "zwemmen",
LearntSkillsAndLevelOfSkills = "C#:4, SQL:4, PLSQL:4, HTML:2, CSS:2, jQuery:1",
Name = "Jasmine Test",
PhoneNr = "0612345678",
ProfileImage = "/Images/hollemar.jpg",
SkillsToLearn = "ASP.net:2, JAVA:1",
Stand = "",
Summary = "",
YearsOfWorkExperience = 11,
PeopleManagerApproved = true,
PeopleManager = "Richard"
};
var data = new List<Person> { test, test1, test2 }.AsQueryable();
var dbSetMock = new Mock<IDbSet<Person>>();
dbSetMock.Setup(m => m.Provider).Returns(data.Provider);
dbSetMock.Setup(m => m.Expression).Returns(data.Expression);
dbSetMock.Setup(m => m.ElementType).Returns(data.ElementType);
dbSetMock.Setup(m => m.GetEnumerator()).Returns(() => data.GetEnumerator());
var mockContext = new Mock<PersonDBContext>();
mockContext.Setup(x => x.Persons).Returns(dbSetMock.Object);
mockContext.Setup(x => x.Persons.Find(1)).Returns(test);
var service = new PersonController(mockContext.Object);
var controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(t => t.HttpContext.Session["loggedinuser"]).Returns(10);
service.ControllerContext = controllerContext.Object;
ViewResult detailspageresultcorrect = (ViewResult) service.Details(10);
Person resultpersoncorrect = (Person) detailspageresultcorrect.Model;
Assert.IsTrue(resultpersoncorrect.Name.Equals(test.Name));
The part where it goes wrong is on the line of: var mockContext = new Mock();
You should be able to mock the Find method directed on the IDbSet instead of going via the Persons property.
So your setups would look like the following:
var dbSetMock = new Mock<IDbSet<Person>>();
dbSetMock.Setup(m => m.Provider).Returns(data.Provider);
dbSetMock.Setup(m => m.Expression).Returns(data.Expression);
dbSetMock.Setup(m => m.ElementType).Returns(data.ElementType);
dbSetMock.Setup(m => m.GetEnumerator()).Returns(() => data.GetEnumerator());
dbSetMock.Setup(m => m.Find(1)).Returns(test);
var mockContext = new Mock<PersonDBContext>();
mockContext.Setup(x => x.Persons).Returns(dbSetMock.Object);

Resources