my code is .
class Program
{
static NorthwindEntities context = new NorthwindEntities();
static void Main(string[] args)
{
Order updateorder = new Order() { OrderID = 10256, ShipCountry = "444", ShipCity = "tehran" };
update(updateorder);
}
static public void update(Order updateorder)
{
context.Entry<Order>(updateorder ).State = EntityState.Modified;
context.SaveChanges();
}
}
please help me , i confused why the detached entity is modified in database
the call to context.Entry<Order>(updateorder) attaches the object to EF.
Related
Trying to use Robolectric 4.3.1 to do the most basic of Android actions, get the Context.
I get a non-null context by doing this (tried many other attempts but all end up getting context = null):
Context context = RuntimeEnvironment.systemContext;
I can pass the object into some methods but I can never use it.
If I try
File dir = context.getFilesDir();
I get
java.lang.RuntimeException: No data directory found for package android
What am I doing wrong?
Here is my code:
#RunWith(RobolectricTestRunner.class)
#Config(sdk = Build.VERSION_CODES.O)
public class BtScannerTests
{
private BluetoothAdapter bluetoothAdapter;
#Before
public void setUp() throws Exception
{
bluetoothAdapter = Shadow.newInstanceOf(BluetoothAdapter.class);
}
private static boolean done = false;
#Test
public void testBtScannerCycle() throws InterruptedException
{
IntermediaryCallback intermediaryCallback = new IntermediaryCallback()
{
#Override
public void onReceiveMdsIntermediary(MdsIntermediary mds, int connectionHandle)
{
}
#Override
public void onReceiveMetricIntermediaries(List<MetricIntermediary> metricList, MdsIntermediary mds, int connectionHandle)
{
}
};
StatusEventCallback statusEventCallback = new StatusEventCallback()
{
#Override
public void onStatusEvent(StatusEvent statusEvent, int connectionHandle, String btAddress)
{
System.out.println("Status event " + statusEvent.name());
if(statusEvent == StatusEvent.SCANNING_PAUSED);
{
done = true;
}
}
};
Context context = RuntimeEnvironment.systemContext;
File dir = context.getFilesDir(); // This is the code that fails; put here to test attempts
AndroidBtManager.setStatusEventCallback(statusEventCallback);
AndroidBtManager androidBtManager =
new AndroidBtManager(context, intermediaryCallback, false, false, true);
BtScanner btScanner = androidBtManager.getBtScanner();
while(!done)
{
Thread.sleep(1000);
}
}
}
Use this one.
Context context = ApplicationProvider.getApplicationContext();
instead of
Context context = RuntimeEnvironment.systemContext;
I have a DynamoDB table with a primary key (id : integer) and secondary key (dateTo : String). I've made a Class that utilizes DynamoDBMapper:
#DynamoDBTable(tableName="MyItems"
public class MyItemsMapper {
private int id;
private String dateTo;
private String name;
#DynamoDBHashKey(attributeName="id")
public void setId(int id) { this.id = id; }
public int getId() { return id; }
#DynamoDBAttribute(attributeName="dateTo")
public void setDateTo(String dateTo) { this.dateTo = dateTo; }
public String getDateTo() { return dateTo; }
#DynamoDBAttribute(attributeName="name")
public void setName(String name { this.name = name; }
public String getName() { return name; }
public boolean saveItem(MyItemsMapper item) {
try {
DynamoDBMapper mapper = new DynamoDBMapper(client); //<-- This connects to the DB. This works fine.
item.setId(generateUniqueNumber()); //<-- This generates a unique integer. Also seems to work fine.
mapper.save(item);
logger.info("Successfully saved item. See info below.");
logger.info(item.toString());
return true;
} catch (Exception e) {
logger.error("Exception while trying to save item: " + e.getMessage());
e.printStackTrace();
return false;
}
}
}
I then have a manager class that uses the bean above, like so:
public class MyManager {
public boolean recordItem(
int id,
String dateTo,
String name,
) {
MyItemsMapper myItemsMapper = new MyItemsMapper();
myItemsMapper.setId(id);
myItemsMapper.setDateTo(dateTo);
myItemsMapper.setName(name);
myItemsMapper.saveItem(myItemsMapper);
}
}
I am running the manager class in a JUnit test:
public class MyManagerTest {
#Test
public void saveNewItemTest() {
MyManager myManager = new MyManager();
myManager.recordItem(1234567, "2018-01-01", "Anthony");
}
}
When I use the saveItem method above via my manager by running my JUnit test, I get the following error:
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: MyItemsMapper; no mapping for HASH key
Not really sure what it's pertaining to, as I definitely have a primary key for my table and my secondary key always has a value as well.
How do I get this to work?
More Info:
It's worth noting that I can record data into my DynamoDB table via the Item object. If I do the below, my data gets recorded into the database:
DynamoDB dynamoDB = new DynamoDBClient().connectToDynamoDB(); //<--
Connection. Works fine.
Table table = dynamoDB.getTable("MyItems");
item.withPrimaryKey("id", 1234567);
item.withString("dateTo", "2018-01-01");
item.withString("name", "Anthony");
PutItemOutcome outcome = table.putItem(item);
However, I'm trying to use DynamoDBMapper because I'm reading that it is a more organized, better way to access data.
Im not sure if this is causing the problem, but you are creating the myItemsMapper object, then passing a reference to this object to itself.
I would suggest removing your saveItem method. The MyItemsMapper class should be a plain old java object. Then make MyManager like this
public class MyManager {
public boolean recordItem(
int id,
String dateTo,
String name,
) {
MyItemsMapper myItemsMapper = new MyItemsMapper();
myItemsMapper.setId(id);
myItemsMapper.setDateTo(dateTo);
myItemsMapper.setName(name);
DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(myItemsMapper);
}
}
If you particularly want to keep the saveItem method make it like this
public boolean saveItem() {
try {
DynamoDBMapper mapper = new DynamoDBMapper(client);
mapper.save(this);
logger.info("Successfully saved item. See info below.");
logger.info(this.toString());
return true;
} catch (Exception e) {
logger.error("Exception while trying to save item: " + e.getMessage());
e.printStackTrace();
return false;
}
}
And then in MyManager do
MyItemsMapper myItemsMapper = new MyItemsMapper();
myItemsMapper.setId(id);
myItemsMapper.setDateTo(dateTo);
myItemsMapper.setName(name);
myItemsMapper.saveItem();
I've the following services:
public with sharing class LibraryService {
public static void remove(String jsonString) {
Library__c library = [ SELECT Id, ilms__Library_Name__c FROM ilms__Library__c WHERE Id = libraryId ] ;
AccessService.deleteReviewerGroup(library);
delete library;
}
}
AccessService class
public with sharing class AccessService {
public static void deleteLibraryReviewerGroup(Library__c library) {
List<Library__Share> reviewersGroups = [ SELECT UserOrGroupId FROM ilms__Library__Share WHERE AccessLevel = 'Read' AND ParentId = :library.Id ];
System.debug('reviewersGroups: ' + reviewersGroups);
if(reviewersGroups.size() == 1) {
String reviewersGroupId = reviewersGroups[0].UserOrGroupId;
delete reviewersGroups;
AccessService.deleteReviewerGroup(reviewersGroupId);
}
return;
}
#future
public static void deleteReviewerGroup(String groupId) {
List<Group> reviewerGroup = [ SELECT Id FROM Group WHERE Id = :groupId ];
delete reviewerGroup;
}
}
Now, when I try to test the LibraryService remove method, I keep receiving the below error:
first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa).
#isTest(SeeAllData=true)
private class TestLibrary {
static testMethod void testRemoveLibrary() {
Library__c library = new Library__c(...);
Boolean isRemoved = LibraryService.remove(TestUtilsClass.idJson(library.Id));
System.assertEquals(isRemoved, true);
}
}
I tried adding Test.startTest() and Test.stopTest() to the testRemoveLibrary method, but I still get the same error. Am I doing something wrong? How do I fix this?
#isTest(SeeAllData=true)
private class TestLibrary {
static testMethod void testRemoveLibrary() {
Library__c library = new Library__c(...);
Test.start();
Boolean isRemoved = LibraryService.remove(TestUtilsClass.idJson(library.Id));
Test.stop();
System.assertEquals(isRemoved, true);
}
}
Please add Test.start and stop including your method.
I'm new to Entity Framework, and I'm think there is something that I misunderstand here.
I'm trying to insert a row in a table, and everywhere I found code example, they call the method InsertOnSubmit(), but the problem is that I can't find anywhere the method InsertOnSubmit, or SubmitChanges.
The error tell me:
System.Data.Object.ObjectSet do not contain the definition for InsertOnSubmit, ...
What I'm doing wrong??
http://msdn.microsoft.com/en-us/library/bb763516.aspx
GMR_DEVEntities CTX;
CTX = new GMR_DEVEntities();
tblConfig Config = new tblConfig { ID = Guid.NewGuid(), Code = "new config code" };
CTX.tblConfigs.InsertOnSubmit(Config); // Error here
Edit:
Using Visual Studio 2010 on FW 4.0
InsertOnSubmit is a Linq-to-SQL method and not in the Entity Framework.
However, since our project was a conversion from Linq-to-SQL we have some extension methods that might help:
public static class ObjectContextExtensions
{
public static void SubmitChanges(this ObjectContext context)
{
context.SaveChanges();
}
public static void InsertOnSubmit<T>(this ObjectQuery<T> table, T entity)
{
table.Context.AddObject(GetEntitySetName(table.Context, entity.GetType()), entity);
}
public static void InsertAllOnSubmit<T>(this ObjectQuery<T> table, IEnumerable<T> entities)
{
var entitySetName = GetEntitySetName(table.Context, typeof(T));
foreach (var entity in entities)
{
table.Context.AddObject(entitySetName, entity);
}
}
public static void DeleteAllOnSubmit<T>(this ObjectQuery<T> table, IEnumerable<T> entities) where T : EntityObject, new()
{
var entitiesList = entities.ToList();
foreach (var entity in entitiesList)
{
if (null == entity.EntityKey)
{
SetEntityKey(table.Context, entity);
}
var toDelete = (T)table.Context.GetObjectByKey(entity.EntityKey);
if (null != toDelete)
{
table.Context.DeleteObject(toDelete);
}
}
}
public static void SetEntityKey<TEntity>(this ObjectContext context, TEntity entity) where TEntity : EntityObject, new()
{
entity.EntityKey = context.CreateEntityKey(GetEntitySetName(context, entity.GetType()), entity);
}
public static string GetEntitySetName(this ObjectContext context, Type entityType)
{
return EntityHelper.GetEntitySetName(entityType, context);
}
}
Where EntityHelper is as per the MyExtensions open source library.
Hello this works for me
Entity db = new Entity();
TABLE_NAME table = new TABLE_NAME
{
COLUMN1 = "TEST",
cOLUMN2 = "test"
//etc...
};
db.TABLE_NAME.Add(table);
db.SaveChanges();
Finally found what was wrong, my Entity database was a dbmx file and not a dbml file. I do not understand why this .. but has long as it work. (Need to buy a new book I guess) – Hugo Feb 17 at 19:40
i also have the same problem .we can insert by using Add
GMR_DEVEntities CTX;
CTX = new GMR_DEVEntities();
tblConfig Config = new tblConfig { ID = Guid.NewGuid(), Code = "new config code" };
CTX.tblConfigs.Add(Config);
I have a Silverlight application and I wanted to be able to get data (customers, orders etc) from a database.
Right now I have managed to create in the Server Application a WCF RIA service, which I can call in the client application and get the required data. The problem is that as far as I understood, the Domain Service calls are asynchronous and therefore I have to create a callback, something like this (so, this part of the code works fine)
private void CountriesCallback(LoadOperation<Country> e)
{
List<Country> countries = e.Entities.ToList();
// add the list as a data source
}
private void ShowAllCountriesButton_Click(object sender, RoutedEventArgs e)
{
MyDomainContext context = new MyDomainContext();
Action<LoadOperation<Country>> callbackCountries = new Action<LoadOperation<Country>>(CountriesCallback);
context.Load(context.GetCountriesQuery(), callbackCountries, null);
}
So this way I have a messy code in the silverlight code-behind classes, with callbacks and async methods. That's why I wanted to create some wrapper classes - with a few static methods that could return just the data that I need, something like this:
public class CountriesBL
{
public static int Add(string countryName, string countryCode)
{
MyDomainContext context = new MyDomainContext();
Country c = new Country() { Name = countryName, Code = countryCode };
context.Countries.Add(c);
SubmitOperation submitOp = context.SubmitChanges();
// always returns 0 !!!
return c.CountryId;
}
private static void RemoveCountryCallback(LoadOperation<Country> e)
{
Country c = e.Entities.FirstOrDefault();
if (c != null)
{
Remove(c);
}
}
public static void Remove(int countryID)
{
MyDomainContext context = new MyDomainContext();
EntityQuery<Country> query =
from c in context.GetCountriesQuery()
where c.CountryId == countryID
select c;
Action<LoadOperation<Country>> callbackCountries = new Action<LoadOperation<Country>>(RemoveCountryCallback);
context.Load(query, callbackCountries, null);
}
public static void Remove(Country country)
{
MyDomainContext context = new MyDomainContext();
context.Countries.Remove(country);
context.SubmitChanges();
}
public List<Country> GetAll()
{
//how can I return the collection knowing that the call is Async??
}
}
But as you can see, I cannot return the inserted country's id, nor can I return the list of countries with this approach. Have you any other idea or suggestion on how to create a wrapper class? It would be really strange to have to use a lot of application logic and even data access elements in the user interface.
Thanks